By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
John Ladasky <la*****@my-deja.comwrites:
I want to know what is the *recommended* way to integrate my own
personal modules with Python. Thanks!
You want the 'distutils' documentation
<URL:http://www.python.org/doc/lib/module-distutilsand the documents
that it references, which will lead you to write a 'setup.py' module
for your package.

Then, it's a matter of running 'python ./setup.py install' to install
your package on a particular system.

--
\ "I hope if dogs ever take over the world, and they chose a |
`\ king, they don't just go by size, because I bet there are some |
_o__) Chihuahuas with some good ideas." -- Jack Handey |
Ben Finney
Jun 27 '08 #2

P: n/a
On Jun 3, 6:52 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
John Ladasky <lada...@my-deja.comwrites:
I want to know what is the *recommended* way to integrate my own
personal modules with Python. Thanks!

You want the 'distutils' documentation
<URL:http://www.python.org/doc/lib/module-distutilsand 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.
Jun 27 '08 #3

P: n/a
On Tue, 03 Jun 2008 17:57:07 -0700, John Ladasky wrote:
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 just make some directory outside of the python source, say
/usr/local/lib/python. I then add that to my PYTHONPATH.
Now the permissions are not a problem, and the libs are
automatically available to any version of python on the system.

** Posted from http://www.teranews.com **
Jun 27 '08 #4

P: n/a

"John Ladasky" <la*****@my-deja.comwrote in message
news:49**********************************@m44g2000 hsc.googlegroups.com...
| 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.

Jun 27 '08 #5

P: n/a
John Ladasky <la*****@my-deja.comwrote in
news:49**********************************@m44g2000 hsc.googlegroups.
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.

--
rzed
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.