954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Where to place modules?

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

Mr.popo
Newbie Poster
12 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

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')
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

Thanks for the advice

Mr.popo
Newbie Poster
12 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Sorry for the double post.

I did this:
[code]
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?

Mr.popo
Newbie Poster
12 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

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

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

Oh I see now. ok thankyou.

Mr.popo
Newbie Poster
12 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You