David Michael Gang
David Michael Gang

Reputation: 7299

configure python path for open source project

Given a github project with the following structure:

mylib/
      package1/
      package2/
      ...
      packagen/

scripts/
        x/a.py
        x/b.py
        .....
        .....
        x/z.py

In the scripts directory there many scripts in many subfolders. In these scripts the packages of mylib are used.

The problem is that somehow we have to tell python where the lib directories are. Otherwise i cannot import the modules.

The solutions in the web i found are not satisfying

Solution 1: Use the sys library:

import sys
sys.path.insert(0,"..\..")
from mylib.package1 import classX

This is not good because i have to write it in every script and if i change the directory structure everything has to be modified

Solution2: Change the PYTHONPATH

This is also not good because i want to collaborate with my friends and i don't want to tell them to change the python path

Is there a better solution?

Upvotes: 0

Views: 75

Answers (1)

Bartosz Marcinkowski
Bartosz Marcinkowski

Reputation: 6861

Create a virtualenv, install all the packages in that virtualenv and execute all the scripts using it.

You could provide a requirements.txt file or a script that creates the virtualenv, so that it is easy for your friends to use it.

Here is a tutorial.

Upvotes: 1

Related Questions