简体   繁体   中英

Python intra-package imports failing

This is my project structure

|--project/
    |--__init__.py
    |--package1/
        |--__init__.py
        |--module1.py
    |--package2/
        |--__init__.py
        |--module2.py

In module1.py I do from project.package2 import module2

The output: ModuleNotFoundError: No module named project

Relative imports don't work either.

I am using Python 3.8 on Ubuntu 20 and have tried with or without __init__ files.

Any ideas why the way the official docs explain how to do imports doesn't work?

So the problem was actually the sys.path

When importing a package the interpreter searches through the directories on sys.path list looking for it as per the documentation says.

The solution to this problem is to add my package's path to my PYTHONPATH environment variable or append my path directly to sys.path

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