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?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
What I could suggest here is trying to install the module first:
pip3 install config
Then run it again and see how it goes.
Hope that this helps! Regards, Bobby
Heya,
Here is a detailed response on how you can troubleshoot such issues in the future
The error message ModuleNotFoundError: No module named 'config' means that Python can’t find a module named ‘config’ in the directories it searches for modules.
Here are some things you might want to check:
File Structure: As you’ve mentioned, both the application.py and config.py are in the same directory. Double-check your file structure on DigitalOcean to make sure this is indeed the case.
Virtual Environment: Ensure that you’re running your application in the correct virtual environment where Flask is installed. The application should be run from within the virtual environment that has Flask installed.
init.py: In some cases, especially in Python 3, having an __init__.py file in the same directory as your .py files may solve this issue. __init__.py is used to indicate that a directory should be treated as a Python package.
Python PATH: Check your Python path. You can print out your Python path from your script using:
pythonCopy code
import sys print(sys.path)
Your current directory should be in the printed output.
Circular Imports: Be aware of circular imports. If config.py also imports application.py, it may cause this error.
Permissions: Make sure that the user running the Python interpreter has read permissions for all the relevant files.
Name Collision: Ensure there is no name collision. If there is a config module in Python’s standard library or in some package you’ve installed, that could be causing a conflict.
Try these steps and see if any of them resolves your issue. If not, there could be something else specific to your environment or setup that’s causing the problem.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.