.pth files in working directory

P

Peter Maas

My goal is to have the top level of a directory tree in the Python
path without touching anything outside the directory. I tried to
create .pth files with the top level path in every subdirectory
but this doesn't work despite working directory being part of the
Python path.

Creating the pth file in .../site-packages works but I prefer to
have everything inside the directory tree so that removing the tree
is sufficient for a complete uninstall. Any hints are appreciated,
thanks.
 
P

Peter Hansen

Peter said:
My goal is to have the top level of a directory tree in the Python
path without touching anything outside the directory. I tried to
create .pth files with the top level path in every subdirectory
but this doesn't work despite working directory being part of the
Python path.

Creating the pth file in .../site-packages works but I prefer to
have everything inside the directory tree so that removing the tree
is sufficient for a complete uninstall. Any hints are appreciated,
thanks.

Not sure from the above description exactly what it is you want, but
generally such non-standard sys.path and .pth manipulations are best
handled by a sitecustomize.py file, possibly which makes its own calls
to site.addsitedir() and such. Try "help(site)" for more.

In this case, I think you'd have a sitecustomize.py which looks for .pth
files in the current directory and calls site.addsitedir(), but I'm not
sure. Maybe if this doesn't work, an example would clarify things.

-Peter
 
P

Peter Maas

Peter said:
Not sure from the above description exactly what it is you want,

I want a tree

top/
install.py
sub1/
__init__.py
mod1.py
sub2/
mod2.py

where I can do "from sub1 import mod1" in mod2.py no matter what the
absolute path of top is. To achieve this I start install.py once to
retrieve the absolute dir of itself (= abspath of top/) and creates
..pth files with its absolute dir in every subdirectory.
> but
generally such non-standard sys.path and .pth manipulations are best
handled by a sitecustomize.py file, possibly which makes its own calls
to site.addsitedir() and such. Try "help(site)" for more.

But sitecustomize.py changes the Python installation, doesn't it?
This wouldn't be an advantage over putting a .pth file into
..../site-packages.
 
M

Michael Ekstrand

I want a tree

top/
install.py
sub1/
__init__.py
mod1.py
sub2/
mod2.py

where I can do "from sub1 import mod1" in mod2.py no matter what the
absolute path of top is. To achieve this I start install.py once to
retrieve the absolute dir of itself (= abspath of top/) and creates
.pth files with its absolute dir in every subdirectory.

If top/ is the working directory for your Python interpreter, the
problem is solved automatically. Python puts the current working
directory in the default search path. So, if you run

python sub2/mod2.py

then it would work.

HTH,
-Michael
 
P

Peter Hansen

Peter said:
But sitecustomize.py changes the Python installation, doesn't it?
This wouldn't be an advantage over putting a .pth file into
.../site-packages.

You can have a local sitecustomize.py in the current directory, which
wouldn't change the Python installation. Would that help?

As far as I can tell, the current directory isn't added to the sys.path
until after site.py executes, so I don't believe you can rely on .pth
files to do anything like what you want. Sitecustomize.py is it, with
appropriate smarts inside.

-Peter
 
P

Peter Maas

Michael said:
If top/ is the working directory for your Python interpreter, the
problem is solved automatically. Python puts the current working
directory in the default search path. So, if you run

IIS sets the the site path as working directory. So I would probably
have to change wd at the beginnig of every module. Each module would
read the top location from a .pth file in its directory. Yes that's
possible. Thanks for your help.
 
P

Peter Maas

Peter said:
You can have a local sitecustomize.py in the current directory, which
wouldn't change the Python installation. Would that help?

I think this is what I need. Thanks, Peter.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top