简体   繁体   中英

Won't import local file as module

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:

Traceback (most recent call last):
    File "application.py", line 11, in <module>
        from config import *
ModuleNotFoundError: No module named 'config'

What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.

I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.

Your thoughts on why it can't find config?

我上面问题的解决方案似乎是运行 Python shell 并将路径添加到config.py所在的目录中(即使它与 application.py 位于同一文件夹中...)以下命令:

sys.path.append("/path/to/config/")

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