What is the best way of distributing a Python script with few pure-python modules? All this should be easy to install to a system without
venv. Ideally by just copying or extracting few text files and directories.
Currently I put the script to a
bin directory available in my
PATH and in the same directory I create
lib_python sub-directory for storing the library modules.
In my script I currently use:
import os import sys # my other regular imports lib_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib_python') sys.path.insert(0, lib_dir) # my imports from the distributed modules: # pylint: disable=wrong-import-position import my_namespace.my_library # noqa: E402
This way seems to be useable so far. There are few drawbacks though:
- Not all imports are at the beginning of the file. I have to use linter directives to silence the complains.
- Linters and/or language servers can have problems with imports from dynamic locations like that.
- A directory name defined by Python standards would be much better instead of inventing my own.
- For simplicity I would like to avoid using wrapper scripts to start my scripts. But if you can convince me that this way is better I will accept that
- I do not want to edit the files (for example to add the current path of the library directory) as part of the installation.
- I can imagine there could be problems when one library module needs to use another library module.