How do I use a relative path in a Python module when the CWD has changed?

Store the absolute path to the module directory at the very beginning of the module: package_directory = os.path.dirname(os.path.abspath(__file__)) Afterwards, load your resources based on this package_directory: font_file = os.path.join(package_directory, ‘fonts’, ‘myfont.ttf’) And after all, do not modify of process-wide resources like the current working directory. There is never a real need to change the working …

Read more

Python: Multiple packages in one repository or one package per repository?

One aspect is covered here https://pip.readthedocs.io/en/stable/reference/pip_install/#vcs-support (Updated link: https://pip.pypa.io/en/stable/topics/vcs-support/) In particular, if setup.py is not in the root directory you have to specify the subdirectory where to find setup.py in the pip install command. So if your repository layout is: pkg_dir/ setup.py # setup.py for package pkg some_module.py other_dir/ some_file some_other_file You’ll need to use …

Read more

How to write a minimally working pyproject.toml file that can install packages?

my question differ because I ask for a human-written pyproject.toml First, the pyproject.toml file is always “human-writable“. Then, it is important to know that in this context setuptools and Poetry take the role of what are called “build back-ends” (in a “PEP 517” sense), and there are many such build back-ends available today, setuptools and …

Read more

How to import a module from a different folder?

Firstly, this import statement: from models import some_model should be namespaced: # in myproject/backend/backend.py or myproject/api/api.py from myproject.models import some_model Then you will need to get the directory which contains myproject, let’s call this /path/to/parent, into the sys.path list. You can do this temporarily by setting an environment variable: export PYTHONPATH=/path/to/parent Or, preferably, you can …

Read more

How to easily distribute Python software that has Python module dependencies? Frustrations in Python package installation on Unix

We also develop software projects that depend on numpy, scipy and other PyPI packages. Hands down, the best tool currently available out there for managing remote installations is zc.buildout. It is very easy to use. You download a bootstrapping script from their website and distribute that with your package. You write a “local deployment” file, …

Read more

Using pytest with a src layer

Recommended approach for pytest>=7: use the pythonpath setting Recently, pytest has added a new core plugin that supports sys.path modifications via the pythonpath configuration value. The solution is thus much simpler now and doesn’t require any workarounds anymore: pyproject.toml example: [tool.pytest.ini_options] pythonpath = [ “src” ] pytest.ini example: [pytest] pythonpath = src The path entries …

Read more

“pip install –editable ./” vs “python setup.py develop”

Try to avoid calling setup.py directly, it will not properly tell pip that you’ve installed your package. With pip install -e: For local projects, the “SomeProject.egg-info” directory is created relative to the project path. This is one advantage over just using setup.py develop, which creates the “egg-info” directly relative the current working directory. More: docs …

Read more