Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

module_path-1.0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

module_path-1.0.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

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

Hashes for module_path-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9520c328eec33fdfa82ae26c52ec748755de3505d901e98ab4a5010a106f1840
MD5 351c803f15b5cf3779d5f76d309ab381
BLAKE2b-256 61e046411ec387217d119d2d2763d54e6510dbf13078ece1ea07c584d71117c2

See more details on using hashes here.

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

Hashes for module_path-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f0d13df260c13803a03689366f35b114e15bbb15e086a5a7073f9220f2abb047
MD5 5ab3a7c6a5f21c48c4d63451c44fdbf5
BLAKE2b-256 1870c5968c1c2f31ae829550d6e0fdf0d714a04acb76ebc34db8703f6aa3fbc7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page