开发者

Utilizing multiple python projects

I have a python app, that I'm devel开发者_StackOverflow社区oping. There is a need to use another library, that resides in different directory.

The file layout looks like this: dir X has two project dirs:

  • current-project
  • xLibrary

I'd like to use xLibrary in currentProject. I've been trying writting code as if all the sources resided in the same directory and calling my projects main script with:

PYTHONPATH=.:../xLibrary ./current-project.py

but this does not work. I'd like to use its code base without installing the library globaly or copying it to my project's directory. Is it possible? Or if not, how should I deal with this problem.


It's generally a good programming practice to isolate packages into actual packages and treat them as such. If you're sure you'd like to continue with that approach though you can modify the search path from within python via:

import sys
sys.path.append( "<path_containing_the_other_python_files>" )

To avoid embedding absolute paths, you can use os.path.abspath(__file__) to obtain the absolute path to the currently executing .py file and follow up with a few os.path.dirname() calls to construct the proper relative path for inclusion to sys.path

A slightly altered approach that would allow you to get the best of both worlds would be to add an __init__.py file to xLibrary's directory then add the path containing 'xLibrary' to sys.path instead. Subsequent Python code could then import everything "properly" via from xLibrary import my_module rather than just import my_module which could be confusing to people accustomed to the standard package directory layout.


This depends how you use xLibrary from current-project.

If you do something like from xLibrary import module1 inside current-project, the xLibrary needs to be laid out as a Python package:

xLibrary/
xLibrary/__init__.py
xLibrary/module1.py  # or whatever other modules the package consists of

In this case, you should include xLibrary's parent directory in PYTHONPATH:

PYTHONPATH=.:.. ./current-project.py

However, if xLibrary is just a collection of Python modules that you import individually (that is, if you do import module1 and import module2 ìn current-project, with xLibrary containing the files module1.py and module2.py), you should include xLibrary in PYTHONPATH just as you did:

PYTHONPATH=.:../xLibrary ./current-project.py


bash$ ln -s ../xLibrary xLibrary


First, it is probably better to use absolute paths in your PYTHONPATH variable. Second, I don't think you need to add current directory to the path.

Other than that, it would be good to know what it is that doesn't work and what the error message is. The command line you have there seems to be missing a semicolon Try these two:

a=12 echo $a
b=12 ;echo $b

...and you'll see the difference.


Apart from the other suggestions, you may consider the virtualenv package. After writing a little setup.py file you can "virtually install" the library and avoid all the PYTHONPATH munging in general.

This is a really good practice and is encouraged by the python community.

Otherwise I prefer the use of sys.path method (by Rakis)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜