Reliably find a modules path even when running an executable.
Project description
Python does not use filenames anymore to support reading from a zip file. This library is deprecated and has been removed from pypi.
Provided functions
files - importlib.resources files this function is the standard for retrieving resources for Python 3.9+
as_file - context manager for retrieving a true filepath for Python 3.9+.
my_dir - Return the directory this module is in. This should exist even in an executable.
my_path - Return the path to the module that called this method. This may not exist in an executable
Example
# my_interface.py
# sdl2 with sld2.dll in package
# File Structure:
# my_sdl/
# sdl2_dll_path/
# SDL2.dll
# __init__.py
# my_interface.py
import os
from module_path import files, as_file
with as_file(files('my_sdl').joinpath('sdl2_dll_path/SDL2.dll')) as sdl_path:
os.environ.setdefault('PYSDL2_DLL_PATH', os.path.dirname(str(sdl_path)))
import sdl2
# Use sdl2
assert sdl2 is not None
check_path/
__init__.py
main.py
check_sub_path
__init__.py
fakedata.txt
# check_path/check_sub_path/__init__.py
import os
import module_path
MY_DIR = module_path.my_dir()
DATA = os.path.join(MY_DIR, 'fakedata.txt')
EXISTS = os.path.exists(DATA)
# check_path/main.py
import module_path
try:
from check_path.check_sub_path import MY_DIR, DATA, EXISTS
except (ImportError, Exception):
from check_sub_path import MY_DIR, DATA, EXISTS
if __name__ == '__main__':
path = module_path.my_path()
directory = module_path.my_dir()
print('path \t', path, module_path.exists(path)) # Should be false with executable
print('dir \t', directory, module_path.exists(directory))
print('sub_dir\t', MY_DIR, module_path.exists(MY_DIR))
print('DATA \t', DATA, EXISTS)
Build the executable with
pyinstaller --name check_path -y --add-data "check_path/check_sub_path/fakedata.txt;check_path/check_sub_path/" check_path/main.py
After pyinstaller builds you will have a dist directory. The data for this example should be stored in dist/check_path/check_path/check_sub_path/fakedata.txt
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file module_path-1.0.1.tar.gz
.
File metadata
- Download URL: module_path-1.0.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9520c328eec33fdfa82ae26c52ec748755de3505d901e98ab4a5010a106f1840 |
|
MD5 | 351c803f15b5cf3779d5f76d309ab381 |
|
BLAKE2b-256 | 61e046411ec387217d119d2d2763d54e6510dbf13078ece1ea07c584d71117c2 |
File details
Details for the file module_path-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: module_path-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0d13df260c13803a03689366f35b114e15bbb15e086a5a7073f9220f2abb047 |
|
MD5 | 5ab3a7c6a5f21c48c4d63451c44fdbf5 |
|
BLAKE2b-256 | 1870c5968c1c2f31ae829550d6e0fdf0d714a04acb76ebc34db8703f6aa3fbc7 |