Skip to content

Instantly share code, notes, and snippets.

@jxramos
Last active October 5, 2019 00:52
Show Gist options
  • Save jxramos/f22cd27abd011faa7964159fa6dc042f to your computer and use it in GitHub Desktop.
Save jxramos/f22cd27abd011faa7964159fa6dc042f to your computer and use it in GitHub Desktop.
Notes about the python includes behavior

PEP 370 -- Per user site-packages directory https://www.python.org/dev/peps/pep-0370/

Site-specific configuration hook¶ https://docs.python.org/3/library/site.html

https://stackoverflow.com/questions/7901373/configuring-python-to-use-additional-locations-for-site-packages https://stackoverflow.com/questions/45528195/import-logic-python-project https://stackoverflow.com/questions/16981921/relative-imports-in-python-3

*.egg-link files for editable packages pip install -e generates this link file in the site-packages. In the package source it also adds these files <packge_name>.egg-info/PKG-INFO <packge_name>.egg-info/not-zip-safe <packge_name>.egg-info/SOURCES.txt <packge_name>.egg-info/top_level.txt <packge_name>.egg-info/dependency_links.txt

Q: is there a means to patch the python import methodology to report some traceability for all the actions it takes to make visible what it's actually doing under the hood. https://docs.python.org/3/reference/import.html https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html https://stackoverflow.com/questions/51639547/understanding-behavior-of-python-imports-and-circular-dependencies https://blog.quiltdata.com/import-almost-anything-in-python-an-intro-to-module-loaders-and-finders-f5e7b15cda47

Q: find the c-code where import is defined and follow its logic https://github.com/python/cpython/tree/master/Lib/importlib https://github.com/python/cpython/blob/master/Modules/_testimportmultiple.c https://github.com/python/cpython/blob/master/Tools/importbench/importbench.py

Packaging hijacking the import to __init__.py when module in package is same name as package https://packaging.python.org/guides/packaging-namespace-packages/ Deleting __init__.py works

https://stackoverflow.com/questions/3365740/how-to-import-all-submodules Highlights technique using pkgutil

#TODO need to cover the case where one of the big packages like scikitlearn or pandas does not import everything under its package and you must manually import some of the subfunctionality that isn't loaded by default.

init.py required for setup's correct functioning for find_packages https://stackoverflow.com/a/56277323/1330381 This is utilized it setuptools.setup packages=setuptools.find_packages(where="src/"),

https://stackoverflow.com/a/29509611/1330381 covers the shorthand allowance you can do in __init__.py for clients to route to your logic without regard to the module structure in the source.

Tricky case where setup uses find_packages, which demands an __init__.py but where a console app at the root of the package source imports the package and a module within which works from a pytest context calling that console app directly as a module, but when called directly from the command line the import fails.

Apparently there's a implicit __init__.py version supported by setup tools that doesn't force you to define an __init__.py file just to have an empty one. https://setuptools.readthedocs.io/en/latest/setuptools.html#find-namespace-packages

The crux of the problem /src/packageName

  • consoleApp.py
  • module.py /tests
  • console_test.py

Need to understand Namespace Packages https://docs.python.org/3/reference/import.html#namespace-packages

Look up entry_points kwarg in setuptools.setup... entry_points={ "console_scripts": ["myExposedCommand = myPackageName.myModule:myMainFunc"], },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment