0

I have a project structure like this:

.
├── .venv
|     └── bin
|          └── python
├── config
│     └── conf.py
├── utils
│   └── test.py
└── requirements.txt

Each of the directories above have an __init__.py file so they should be picked up as a Python package.

I am unable to import config into the utils.test.py script.

The error I encounter is:

File "./utils/test.py", line 2, in <module>
import config.conf
ModuleNotFoundError: No module named 'config'

My Python path is "./.venv/bin/python".

Can someone help me resolve this issue so I can import files from different sub-directories?

Thanks!

Aaron
  • 26
  • 3
  • 1
    Does this answer your question? [Import a file from a subdirectory?](https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory) – EXODIA May 31 '20 at 11:39

2 Answers2

0

Add an empty __init__.py file in the config folder.

Then you can do the following in your test.py

import config.conf as conf

conf.your_config_function()
Nico Müller
  • 1,784
  • 1
  • 17
  • 37
  • 2
    Hi NIco I already have an empty __init__.py in the config directory, as well as all other directories in the workspace. Is there another reason why I could be experiencing this error? – Aaron May 31 '20 at 12:08
0

Add empty __init__.py file to the config directory in order to identify it as a python package. See more here

Updating following the comment - Add the folder path to the system path -

import os
import sys
sys.path.insert(0, os.path.dirname(os.getcwd()))
import config.conf
Tom Ron
  • 5,906
  • 3
  • 22
  • 38
  • 1
    Hi Tom, I already have an empty __init__.py in the config directory, as well as all other directories in the workspace. Is there another reason why I could be experiencing this error? – Aaron May 31 '20 at 12:07