Ive got my own python modules which I need to place somewhere so python can read them. Ive got two questions.
1. Wheres the best place to put modules?
2. It wont let me add modules to /usr/lib/python2.5?

Thanks
Mr.popo

1. /usr/Lib/python2.5 OR /usr/Lib/python2.5/site-packages OR (for specialized modules) in the same directory as the files that will use them.
2. It's probably a file permissions problem.

Jeff

I always tend to stick the main program one directory above all the other modules. My directory tree will look something like this:

Gork/
  gork.py
  README
  ...
  images/
    title.tga
    player1.tga
    player2.tga
    ...
  modules/
    __init__.py
    gameboard.py
    globals.py
    tga32.py
    widgets.py
    ...

And so on. Obviously, "gameboard.py", etc. are the modules. I've wrapped them in a subdirectory-module. To use them, I import them in gork.py thus:

from modules.globals import *
import modules.widgets
...

Hope this helps.

You can put them in a directory in the PYTHONPATH, so Python can find it (be careful with that one):

# show PYTHONPATH
import sys
print sys.path

Or better, add your own module directory (let's say MyModules) to PYTHONPATH. You have to add this all your programs that import one of your modules then:

import sys
sys.path.append(r'C:\Python25\MyModules')

Sorry for the double post.

I did this:

import sys
sys.path.append("/media/disk/usr/lib/python2.5/MyModules")

I didnt get an errors but i checked if the directory was created and nothing was there. Should this happend?

Edited 3 Years Ago by pritaeas: Fixed formatting

sys.path is just the search path that the system uses to look up files whose full pathname is not given.

That is, a statement like

import mymodule1

will search the directories in sys.path for mymodule1.py.

So by itself, your statement would not create a directory. Rather, it would tell Python to add that directory to the list of paths to search.

Try this:

import os
os.mkdir("/media/disk/usr/lib/python2.5/MyModules")

That should make a directory.

BTW, "/media/disk/..." -- your copy of Python isn't on CD-ROM or some other read-only media, is it?

Jeff

Jeff is right, your module directory has to exist with the module(s) you want to import already in it.

This article has been dead for over six months. Start a new discussion instead.