简体   繁体   中英

Python executable script ModuleNotFound

I am trying to better understand importing modules. I read about how to do this from here https://stackoverflow.com/a/14132912/14179793 and I can get it to work using solution 1. There is an additional variable that I need to figure out though.

This is a dummy project I am testing with:

.
├── a_package
│   ├── __init__.py
│   └── lib_a.py
├── b_package
│   ├── __init__.py
│   └── test_script.py
├── main.py
└── src
    └── src_lib
        └── src_lib.py

With this setup I can do:

python -m b_package.test_script
this is lib a function
This is src_lib_function.

test_script.py:

from a_package.lib_a import lib_a_function
from src.src_lib.src_lib import src_lib_function

if __name__ == '__main__':
    lib_a_function()
    src_lib_function()
    pass

The goal is to make b_package/test_script.py executable without using python test_script ie ./test_script

However, adding the shebang at the top #!/usr/bin/env python causes an import error:

$ ./b_package/test_script.py 
Traceback (most recent call last):
  File "./b_package/test_script.py", line 2, in <module>
    from a_package.lib_a import lib_a_function
ModuleNotFoundError: No module named 'a_package'

I assume it is because python is not loading it as a module based off the above mentioned question but I am not sure how to resolve this.

I ended up using setuptools as suggested by kosciej16 to achieve the desired results.

New project structure:

.
├── a_package
│   ├── __init__.py
│   └── lib_a.py
├── b_package
│   ├── __init__.py
│   └── test_script.py
├── main.py
├── pyproject.toml
├── setup.cfg
└── src
    ├── __init__.py
    └── src_lib
        ├── __init__.py
        └── src_lib.py

setup.cfg:

[metadata]
name = cli_test
version = 0.0.1

[options]
packages = find:

[options.entry_points]
console_scripts =
    test_script = b_package.test_script:main

This allows the user to clone the repo and run pip install. from the top level then they can execute the script by just typing test_script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM