Marcin Cylke
Marcin Cylke

Reputation: 2066

Utilizing multiple python projects

I have a python app, that I'm developing. 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:

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.

Upvotes: 1

Views: 251

Answers (5)

Pär Wieslander
Pär Wieslander

Reputation: 28934

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

Upvotes: 2

pygabriel
pygabriel

Reputation: 10008

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)

Upvotes: 0

Mattias Nilsson
Mattias Nilsson

Reputation: 3757

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.

Upvotes: 0

Rakis
Rakis

Reputation: 7864

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.

Upvotes: 2

mawimawi
mawimawi

Reputation: 4333

bash$ ln -s ../xLibrary xLibrary

Upvotes: 0

Related Questions