tags:

views:

356

answers:

3

Hello,

I have a verification project of which Python is only a part. The project's structure is:

root/
  vhdl/
  synthesis/
  tb/
    some_tb1/
  svn_code/
     vhdl/
     python_lib/

generic_svn is a Subversion 'externals' directory holding common code. I have several scripts in tb/some_tb1 that want to use modules from svn_code/python_lib. But too see these modules, and as this isn't a pure Python package hierarchy (so relative imports are useless) I have to somehow update my path.

I started with:

import sys
sys.path.append('../../svn_code/python_lib/')

In all Python files. But that's somewhat of a duplication. So I created a file named 'path_setup.py', placed the code above in it and just imported it.

But perhaps a better method is just to create a .pth file in the tb/some_tb1 directory that points to the modules ?

Which method do you prefer, or are there other good options ? TIA

+3  A: 

lib/site-packages is your friend. So is the PYTHONPATH environment variable.

In Linux, you can use a symbolic link to your SVN checkout, also.

In all environments, you can put .pth files in yolur site-packages.

In all environments, you can set PYTHONPATH.

We use both .pth files and PYTHONPATH environment variable.

  • Some modules are defined via .pth files because they're "installed" for everyone to use.
  • Some modules are defined only via a user's PYTHONPATH because they're part of a complex project, but they're not for everyone to use. Yet. Maybe some day they'll mature.
S.Lott
+1  A: 

I just tried putting a .pth file in directory a with a.py, pointing at directory b, and module a couldn't import the module in b.

Looking at site.py (which processes .pth files) it looks like .pth files in the current directory aren't used. It only picks up those that are in site-packages or ~/site-packages.

You could put one in one of those locations, or as S.Lott suggests, use PYTHONPATH, or stick with what you're doing with the module that adjusts the path.

wilberforce
Yes, I've also noticed that it doesn't work for the current dir. That sucks...
Eli Bendersky
+2  A: 

If you want to use .pth files you must do:

import site
site.addsitedir(os.path.join(base_dir, 'svn_code/python_lib'))

You shouldn't ever use relative directories for paths, but should figure out base_dir based on something fixed (probably __file__).

ianb
Why should I never use relative dirs for paths ?
Eli Bendersky