When to put my modules

B

Bob van der Poel

I'm working on a moderately sized application and I'm planning on having
a fair number of modules. The only question in my mind is: where do I
put the modules ... and how do I let python know about them?

I could put them with the rest of the python lib, but I'm not really
presumtious to do that.

I can keep them all in the current directory, but that forces the user
to chd to a certain location all the time.

Best, I think, is to put them in a program specific location. My program
needs some other data files, etc. so I'm thinking that I can just put
the python stuff in /usr/local/lib/MYPROGRAM/modules.

Okay, but what about when I'm just working on them? In this case I'd
like them to be in my current working dir.

Is there a magic line I need to add to the top of the main module to set
the path up?

Apologies if this is covered in a document...but I can't see to find
anything. Maybe this is a faq?
 
Y

Yermat

Hi,

Bob van der Poel said:
I'm working on a moderately sized application and I'm planning on
having a fair number of modules. The only question in my mind is:
where do I put the modules ... and how do I let python know about
them?

see http://www.python.org/doc/current/tut/node8.html especially the 6.1.1
I could put them with the rest of the python lib, but I'm not really
presumtious to do that.

Not that good...
I can keep them all in the current directory, but that forces the user
to chd to a certain location all the time.

Best, I think, is to put them in a program specific location. My
program needs some other data files, etc. so I'm thinking that I can
just put the python stuff in /usr/local/lib/MYPROGRAM/modules.

Not that good too. You may want to re-use your module later in another
program...
Okay, but what about when I'm just working on them? In this case I'd
like them to be in my current working dir.

Is there a magic line I need to add to the top of the main module to
set the path up?

Apologies if this is covered in a document...but I can't see to find
anything. Maybe this is a faq?

What I'm doing is something like this :
All my module are in a directory such as /home/loic/MyPython/.
I set the PYTHONPATH to that directory.
I've got for example a module rtf in /home/loic/MyPython/rtf/, another one
Heredis in /home/loic/MyPython/Heredis and my program is in
/home/loic/MyPython/Heredis2Gedcom for example.
In that way, I don't mix my module with standard one. Every program can
access all non program specific module.

For other tips, look at sys.path too.

Hope it help !
Yermat
 
B

Bob van der Poel

Yermat said:

Okay...so I just need to add paths to the sys.path variable. Seems
simple enough. Thanks.
Not that good...




Not that good too. You may want to re-use your module later in another
program...

Well, probably not. The modules I'm talking about here are pretty
specific to my program.
What I'm doing is something like this :
All my module are in a directory such as /home/loic/MyPython/.
I set the PYTHONPATH to that directory.
I've got for example a module rtf in /home/loic/MyPython/rtf/, another one
Heredis in /home/loic/MyPython/Heredis and my program is in
/home/loic/MyPython/Heredis2Gedcom for example.
In that way, I don't mix my module with standard one. Every program can
access all non program specific module.

For other tips, look at sys.path too.

Hope it help !
Yermat

Yes, it does help. Just to clearify, the program I'm working on will be
distributed to other users. So, putting the modules in a
my-personal-python-stuff directory isn't really an option. They most
likely need to go in the main python tree (which, again, I don't like),
or in a program specific directory. Setting PYTHONPATH is an option, but
I don't like to force a user to set an env variable just to run my
program. Yes, I could turn my prog into a shell script which sets the
variable, and then runs the *real* program, but I always find that
method to be a bit convoluted.
 
T

Terry Reedy

Okay...so I just need to add paths to the sys.path variable. Seems
simple enough. Thanks.

If your modules are specific to an application, adding the application
module-dir to the front of sys.path is probably best. If your modules
constitute a library that other Python programmers might want to access
(such as pygame or numerical), then the package should go in
lib/site-packages.

TJR
 
B

Bob van der Poel

Terry said:
If your modules are specific to an application, adding the application
module-dir to the front of sys.path is probably best. If your modules
constitute a library that other Python programmers might want to access
(such as pygame or numerical), then the package should go in
lib/site-packages.

Yes, thanks. And I'll find out soon enuf when I try this, but I suppose
that having a bunch of alternates in the path is not a problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top