Is there a convinient tool like Makefile for Python that will append a path to PYTHONPATH?

Question:

I have an issue where I want to add a path to PYTHONPATH. Of course, I don’t want to modify my .bashrc to get just this project running.

PyCharm for example will add to PYTHONPATH the root contents when you create a configuration and as a result when you run the program from within PyCharm it will work.

I was wondering if there is a configuration program like Makefile for Python that you can define configurations like "run this script but also append the current path to PYTHONPATH". An equivalant of what PyCharm does but for the command line.

Asked By: Phrixus

||

Answers:

what you want to do is to use a package that exists outside of your python’s site-packages directory, this can be done without modifying PYTHONPATH , pip has the ability to do that by installing it in editable mode, (which adds it to python’s .pth file)

pip install -e folder_containing_setup_dot_py

a minimal setup.py would look as follows for a simple script folder.

# setup.py

from setuptools import setup

setup(package_dir={'':'src'},packages=['package_name'])

and your project should look as follows:

.
└── root directory of project/
    ├── setup.py
    └── src/
        └── package_name/
            ├── package_file.py
            └── __init__.py

and you could simply import your code from anywhere

from package_name import package_file
Answered By: Ahmed AEK
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.