Python - ModuleNotFoundError: No module named

徘徊边缘 提交于 2021-01-03 06:18:13

问题


I'm new in Python and I'm having the following error with this simple example:

This is my project structure:

python_project
.
├── lib
│   ├── __init__.py
│   └── my_custom_lib.py
└── src
    ├── __init__.py
    └── main.py

And this is the error when I execute the src/main.py file:

☁  python_project  python src/main.py

Traceback (most recent call last):
  File "src/main.py", line 3, in <module>
    from lib import my_custom_lib
ImportError: No module named lib

If I move the main.py file to the root and then I execute this file again, works... but is not working inside src/ directory

This is my main.py:

from lib import my_custom_lib

def do_something(message):
        my_custom_lib.show(message)

do_something('Hello World!')

Note: When I execute the same code from Pycharm is working fine, but not from my terminal.


回答1:


Your PYTHONPATH is set to the current directory from the program you execute. So if you're executing something inside a directory src, it will never be able to find the directory lib because it's outside the path. There's a few choices;

  • Just move lib/ into src/ if it belongs to your code. If it's an external package, it should be pip installed as Henrique Branco mentioned.
  • Have a top-level script outside of src/ that imports and runs src.main. This will add the top-level directory to python path.
  • In src/main.py modify sys.path to include the top-level directory. This is usually frowned upon.
  • Invoke src/main.py as a module with python -m src.main which will add the top-level directory to the python path. Kind of annoying to type, plus you'll need to change all your imports.



回答2:


You are using the from a import b incorrectly. it should look like this:

import lib.my_custom_lib

The other method is used to import certain methods, functions, and classes from a module, not the module itself. To import a specific function from the my_custom_lib module it would look like this:

from lib.my_custom_lib import foo



回答3:


Try using a relative import instead:

from ..lib import my_custom_lib




回答4:


If I may add to MarkM's answer, if you wanted to keep your current directory structure and still make it work, you could add a setup.py in your root dir, where you can use setuptools to create a package you could install.

If your file had something along the lines of:

# setup.py

from setuptools import find_packages, setup

setup(
  name='foo',
  version=`1.0.0`,
  packages=find_packages(),
  entrypoints={
    'console_scripts': [
      'foo=src.main:main',
    ],
  },
)

And then you do pip install [--user] -e path/to/directory you'll get an "editable package" which will effectively a symlink to the package in your development directory, so any changes you make will not require a reinstall (unless of course you rejig package structure or add/remove/edit entry points).

This does assume your src/main.py has a main function.

You'll also need __init__.py files in your "package" directories, even in Python3, as otherwise Python assumes these are namespace packages (Won't go into detail) and the find_packages() call won't find them.

This will also allow your relative imports to work. Absolute imports will only work when invoking the script from your entry point but not when calling the script directly in your development directory.



来源:https://stackoverflow.com/questions/61532337/python-modulenotfounderror-no-module-named

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!