Import, site packages, my modules, Windows vs. Linux

J

John Ladasky

Hi folks,

Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux
7.04 desktop.

I've gotten tired of maintaining multiple copies of my personal
modules that I use over and over. I have copies of these files in the
same directory as the main program I happen to be working on at the
time. I've also downloaded FANN, and want to use its Python
bindings. FANN does not seem to build automatically, like wxWidgets
did.

These two issues have led me to examine exactly how the import
statement works, how the PYTHONPATH environment variable is
constructed, and how to change it.

On Windows I found a solution that works, but which may be a kludge.
In the Python "site-packages" folder, I added a sub-folder called "my-
packages". Then I created a text file, "my-packages.pth", containing
the single line, "my-packages." Finally, I moved my common personal
modules into the my-packages folder and deleted all of my clumsy
duplicates. Import statements now work for all of my files on the
Windows box, great!

I then tried to use this same strategy in Linux, and saw that I don't
automatically have the privileges needed to alter the site-packages
folder. On my Windows box, my default account has Administrator
privileges. On Linux I can, of course, use sudo to modify the site-
packages folder. But the fact that I would have to use sudo has me
asking -- is there something inappropriate, or unsafe in my approach?

I want to know what is the *recommended* way to integrate my own
personal modules with Python. Thanks!
 
J

John Ladasky

You want the 'distutils' documentation
<URL:http://www.python.org/doc/lib/module-distutils> and the documents
that it references, which will lead you to write a 'setup.py' module
for your package.


Many thanks, Ben, distutils was exactly what I needed. It was a
little strange to grasp the concept that I would be "distributing" my
module to myself -- but once I got over that mental hurdle, it worked
perfectly.
 
T

Terry Reedy

| On Windows I found a solution that works, but which may be a kludge.
| In the Python "site-packages" folder, I added a sub-folder called "my-
| packages". Then I created a text file, "my-packages.pth", containing
| the single line, "my-packages." Finally, I moved my common personal
| modules into the my-packages folder and deleted all of my clumsy
| duplicates. Import statements now work for all of my files on the
| Windows box, great!
|
| I then tried to use this same strategy in Linux, and saw that I don't
| automatically have the privileges needed to alter the site-packages
| folder. On my Windows box, my default account has Administrator
| privileges.

If you do the Windows install for all users, all users can add packages to
site-packages -- as long as adding the package directory comprises the
whole installation process and no registry fiddling is required.
 
R

rzed

com:
Hi folks,

Running Python 2.5 on both a Windows XP laptop, and an Ubuntu
Linux 7.04 desktop.

I've gotten tired of maintaining multiple copies of my personal
modules that I use over and over. I have copies of these files
in the same directory as the main program I happen to be working
on at the time. I've also downloaded FANN, and want to use its
Python bindings. FANN does not seem to build automatically,
like wxWidgets did.

These two issues have led me to examine exactly how the import
statement works, how the PYTHONPATH environment variable is
constructed, and how to change it.

On Windows I found a solution that works, but which may be a
kludge. In the Python "site-packages" folder, I added a
sub-folder called "my- packages". Then I created a text file,
"my-packages.pth", containing the single line, "my-packages."
Finally, I moved my common personal modules into the my-packages
folder and deleted all of my clumsy duplicates. Import
statements now work for all of my files on the Windows box,
great!

I then tried to use this same strategy in Linux, and saw that I
don't automatically have the privileges needed to alter the
site-packages folder. On my Windows box, my default account has
Administrator privileges. On Linux I can, of course, use sudo
to modify the site- packages folder. But the fact that I would
have to use sudo has me asking -- is there something
inappropriate, or unsafe in my approach?

I want to know what is the *recommended* way to integrate my own
personal modules with Python. Thanks!

I can't speak about Linux, but on Windows, you really should move
your code out of the Python directory tree, I believe. When you
upgrade, you'll have to be sure to set it up the same way again,
and remember that your code resides there before you wipe out that
directory tree, etc.

Instead, just set up another directory containing your code and add
that to your PYTHONPATH environment variable string. That will be
unaffected by the Python version you're running, and will be
available for multiple versions, if you run more than one.
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top