Where to place modules?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 12
Reputation: Mr.popo is an unknown quantity at this point 
Solved Threads: 0
Mr.popo Mr.popo is offline Offline
Newbie Poster

Where to place modules?

 
0
  #1
Jan 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Where to place modules?

 
0
  #2
Jan 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Where to place modules?

 
0
  #3
Jan 6th, 2008
I always tend to stick the main program one directory above all the other modules. My directory tree will look something like this:
  1. Gork/
  2. gork.py
  3. README
  4. ...
  5. images/
  6. title.tga
  7. player1.tga
  8. player2.tga
  9. ...
  10. modules/
  11. __init__.py
  12. gameboard.py
  13. globals.py
  14. tga32.py
  15. widgets.py
  16. ...
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:
  1. from modules.globals import *
  2. import modules.widgets
  3. ...
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Where to place modules?

 
0
  #4
Jan 6th, 2008
You can put them in a directory in the PYTHONPATH, so Python can find it (be careful with that one):
  1. # show PYTHONPATH
  2. import sys
  3. 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:
  1. import sys
  2. sys.path.append(r'C:\Python25\MyModules')
Last edited by Ene Uran; Jan 6th, 2008 at 2:20 pm. Reason: []
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 12
Reputation: Mr.popo is an unknown quantity at this point 
Solved Threads: 0
Mr.popo Mr.popo is offline Offline
Newbie Poster

Re: Where to place modules?

 
0
  #5
Jan 6th, 2008
Thanks for the advice
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 12
Reputation: Mr.popo is an unknown quantity at this point 
Solved Threads: 0
Mr.popo Mr.popo is offline Offline
Newbie Poster

Re: Where to place modules?

 
0
  #6
Jan 6th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Where to place modules?

 
0
  #7
Jan 6th, 2008
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:

  1. import os
  2. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Where to place modules?

 
0
  #8
Jan 7th, 2008
Jeff is right, your module directory has to exist with the module(s) you want to import already in it.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 12
Reputation: Mr.popo is an unknown quantity at this point 
Solved Threads: 0
Mr.popo Mr.popo is offline Offline
Newbie Poster

Re: Where to place modules?

 
0
  #9
Jan 7th, 2008
Oh I see now. ok thankyou.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC