简体   繁体   中英

ImportError - Module Not Found?

My directory structure is:

project_folder/
..my_project/
....server/
......server.py
......api.py

When I'm in the project_folder and run python3 my_project/server/server.py , I get ModuleNotFoundError: No module named 'my_project' with the line of code:

from my_project.server.api import app as application

Weirdly, when I run my tests with Pytest everything passes. I've been trying to solve my problem without using this code snippet, which I always see recommended to solve these problems:

import sys
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )

Any tips?

您尝试调用的模块已经在同一个程序包中,因此只需执行

from api import app as application

在my_project和服务器文件夹中添加一个空的__init__.py文件。

run this cd my_project/server/ and then python server.py
Or remove this line from server.py import my_project

And if you are trying to make a module add

__init__.py

to your server dir

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