1

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.

Phrixus
  • 1,209
  • 2
  • 19
  • 36
  • ok maybe use a make file and set the pythonpath in it? https://stackoverflow.com/questions/35948154/how-to-set-environment-variables-in-makefile – Andrew Allaire Apr 06 '23 at 19:15

1 Answers1

1

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
Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23