Report this

What is the reason for this report?

Python can't find local files/modules

Posted on January 2, 2020

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

If your config.py file and init.py file on the same path, move config.py file outside of the path and place the same in root.

for example if both are in “APP” directory, move the config.py and place it inside the root director of “APP” directory

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. Circular Imports: Be aware of circular imports. If config.py also imports application.py, it may cause this error.

  6. Permissions: Make sure that the user running the Python interpreter has read permissions for all the relevant files.

  7. 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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.