I'm having trouble understanding the module layout in python. Here is my directory / file structure
Project2.1/
├── project2
│ ├── data_mining
│ │ ├── process.py
│ │ └── __init__.py
│ └── __init__.py
└── tests
├── data
│ └── data.csv
├── data_mining
│ ├── __init__.py
│ └── test_process.py
└── __init__.py
File test_process.py, contains the following import
from project2.data_mining.process import ClassP
Run file tests/data_mining/test_proecss.py using the following command from directory Project2.1
$ cd Project2.1
$ python3 tests/data_mining/test_process.py
Generates the error
File "tests/data_mining/test_process.py", line 7, in <module>
from project2.data_mining.process import ClassP
ModuleNotFoundError: No module named 'project2'
ClassP is a class inside project2/data_mining/process.py
CodePudding user response:
Since you are in the data_mining directory of tests folder , only those files inside the data_mining folder are accessible directly, and you can't type the path of module with from
, you need to add the path of the data_mining folder of project2
, so get the exact path of data_mining (of project2 )
and
import sys
sys.path.append(exact path)
from process import ClassP
this will append the path of that folder and make all files inside it accessible to the import system
also remeber we don't use .py
or any extension while importing
it is just like importing any other module from random import randint
for instance
:D