I have a project with the following structure:
HorticulturalSalesPrediction/
Docker
HorticulturalSalesPrediction_API/
optimization/
__init__.py
optuna_optim.py
preprocess/
__init__.py
base_dataset.py
utils/
__init__.py
FeatureAdder.py
helper_functions.py
__init__.py
optim_pipeline.py
run.py
In the script run.py
I import stuff like this:
import optim_pipeline
from utils import helper_functions
And in the script optim_pipeline.py
I import stuff like this:
from utils import helper_functions
from preprocess import base_dataset
from optimization import optuna_optim
I developed this framework with the IDE PyCharm and when I run it with the 'Run'-Button, the framework works fine. But when I want to run it over a terminal with python3 run.py
or python3 -m run.py
, I get the following error:
Traceback (most recent call last):
File "run.py", line 3, in <module>
import optim_pipeline
File "/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API/optim_pipeline.py", line 4, in <module>
from preprocess import base_dataset
File "/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API/preprocess/base_dataset.py", line 8, in <module>
from HorticulturalSalesPrediction_API.utils import FeatureAdder
ModuleNotFoundError: No module named 'HorticulturalSalesPrediction_API'
I know that there are already tons of questions and solutions to this whole python import topic ( Relative imports - ModuleNotFoundError: No module named x , Call a function from another file? , Relative imports for the billionth time , ...), but none of these worked for me.
When I print sys.path
I among others receive '/home/josef/Schreibtisch/HorticulturalSalesPrediction/HorticulturalSalesPrediction/HorticulturalSalesPrediction_API'
, so all this stuff should be available at the syspath.
I also tried to do relative and absolute imports. But with these attempts I reveice ValueError: attempted relative import beyond top-level package
or ImportError: attempted relative import with no known parent package
errors (eg when I try from. import optim_pipeline
).
Try reinstalling the modules and updating python.
Update HorticulturalSalesPrediction_API
So I found my mistake. The solution is to run python3 -m HorticulturalSalesPrediction_API.run
in the HorticulturalSalesPrediction folder. Then it works like expected. I just then had to adjust some imports:
from HorticulturalSalesPrediction_API.utils import helper_functions
from . import optim_pipeline
and
from HorticulturalSalesPrediction_API.utils import helper_functions
from HorticulturalSalesPrediction_API.preprocess import base_dataset
from HorticulturalSalesPrediction_API.optimization import optuna_optim
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.