Search code examples
pythonsetup.pysite-packages

how to locate source code path from module


I have a python package built from source code in /Document/pythonpackage directory

/Document/pythonpackage/> python setup.py install

This creates a folder in site-packages directory of python

import pythonpackage
print(pythonpackage.__file__)
>/anaconda3/lib/python3.7/site-packages/pythonpackage-x86_64.egg/pythonpackage/__init__.py

I am running a script on multiple environments so the only path I know I will have is pythonpackage.__file__. However Document/pythonpackage has some data that is not in site-packages is there a way to automatically find the path to /Document/pythonpackage given that you only have access to the module in python?


Solution

  • working like that is discouraged. it's generally assumed that after installing a package the user can remove the installation directory (as most automated package managers would do). instead you'd make sure your setup.py copied any data files over into the relevant places, and then your code would pick them up from there.

    assuming you're using the standard setuptools, you can see the docs on Including Data Files, which says at the bottom:

    In summary, the three options allow you to:

    include_package_data

    Accept all data files and directories matched by MANIFEST.in.

    package_data

    Specify additional patterns to match files that may or may not be matched by MANIFEST.in or found in source control.

    exclude_package_data

    Specify patterns for data files and directories that should not be included when a package is installed, even if they would otherwise have been included due to the use of the preceding options.

    and then says:

    Typically, existing programs manipulate a package’s __file__ attribute in order to find the location of data files. However, this manipulation isn’t compatible with PEP 302-based import hooks, including importing from zip files and Python Eggs. It is strongly recommended that, if you are using data files, you should use the ResourceManager API of pkg_resources to access them