0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How can in install the packages in mylib? Furthermore does it mean that everytime i change a module i will have to run the install command?
Create a setup.py for your libraries and use pip install -e <local project path>. After you change a library, the version number should increase and then running pip install --upgrade -e <local project path or requirements.txt file> will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.