简体   繁体   中英

Error when importing modules from different folders in Python

I have the following:

my_project/
    hybrik/
        __init__.py
        models/
            __init__.py
            builder.py
    scripts/
        demo.py

And in demo.py:

from hybrik.models import builder

When I tried to run demo.py, an error occured:

ModuleNotFoundError: No module named 'hybrik'

I've already had __init__.py , why can't it find the module?

Python will look for modules in locations on the PYTHONPATH .

Assuming the actual code in those 4 Python files makes sense, you can do the following:

  • on PowerShell:
$env:pythonpath += ";/path/to/my_project"
  • on Windows command prompt:
set PYTHONPATH=%PYTHONPATH%;/path/to/my_project
  • on a Linux shell:
PYTHONPATH=$PYTHONPATH:/path/to/my_project

Alternatively, you can build a package and install it in the environment of your 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