Ive got a directory structure:
Business Logic
excel_format_error_checks.py
tests
test_excel_format_error_checks.py
When I set my import of excel_format_error_checks like below, VSCode test discovery gives an error
ModuleNotFoundError: No module named 'Business_Logic'
test_excel_format_error_checks.py
import sys
sys.path.append('..')
from Business_Logic import excel_format_error_checks
class TestExcelFormatErrorChecks(TestCase):
#patch('Business_Logic.excel_format_error_checks.openpyxl')
def test_tgl_not_missing_from_titles(self,mock_openpyxl):
...
If I change the import to from ..Business_Logic import excel_format_error_checks, the test discovery works.
When I then try to run the tests from VSCode test discovery, I get an error because of the #patch path, ModuleNotFoundError: No module named 'Business_Logic'.
When I try to run the tests from the terminal, I get the error
ImportError: Failed to import test module: test_excel_format_error_checks Traceback (most recent call last): File "C:\Users\brady\AppData\Local\Programs\Python\Python310\lib\unittest\loader.py", line 154, in loadTestsFromName module = __import__(module_name) File "C:\Users\brady\Desktop\excel_formatting\excel_format_website\api\tests\test_excel_format_error_checks.py", line 6, in <module> from ..Business_Logic import excel_format_error_checks ImportError: attempted relative import with no known parent package
Questions, 1.why does test discovery only work when i add the .. to the import
2. How can I fix the issue/ fix the patch path
Thanks
In the python language, the import usually only looks in the current directory of the file, and your file directory is obviously not in the same folder.
We can use the following code to provide relative paths. Of course, absolute paths are more commonly used.
import os
import sys
os.path.join(os.path.dirname(__file__), '../')
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from BusinessLogic import excel_format_error_checks
Related
app-main-folder
/local
/__init__.py
/run.py
constants.py
I am trying to import from constants in run.py it's throwing this error
Traceback (most recent call last):
File "local/run.py", line 4, in <module>
from init import app
File "/home/manavarthivenkat/ANUESERVICES--BACKEND/local/init.py", line 5, in <module>
from constants import BaseConstants
ModuleNotFoundError: No module named 'constants'
pip install constants
Try this on your shell
and try running run.py
make sure you load the constants library
this is because Python assumes all your python files are in the current directory. you should tell the compiler you are looking for the file somewhere else.
from app-main-folder.constants import constants # assuming constants is the name of your class in that constants.py
You can use sys.path.append to tell Python interpreter where to search the modules.
First, you can check the current Python path
import sys
from pprint import pprint
pprint(sys.path)
If the path to the directory of constants.py is not included in the output, you can manually add that path, by
import sys
sys.path.append("/path/to/constants.py")
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 (e.g. 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
I am attempting to run a script which calls another python file (copied along with its entire github repo), but I am getting a ModuleNotFoundError:
This is despite putting the files into the correct directories.
How I run it
Activate my python3.9 virtual environment
(newpy39) aevas#aevas-Precision-7560:~/Desktop/Dashboard_2021_12$ python3 dashboard.py
where aevas is my username, and ~/Desktop/Dashboard_2021_12 is the folder
No additional arguments required to run it.
Imports for dashboard.py
import sys
sys.path.insert(1, 'targetTrack')
# Qt imports
from PyQt5.QtCore import QThread, pyqtSignal
import argparse
import configparser
import platform
import shutil
import time
import cv2
import torch
import torch.backends.cudnn as cudnn
from yolov5.utils.downloads import attempt_download
from yolov5.models.experimental import attempt_load
from yolov5.models.common import DetectMultiBackend
from yolov5.utils.datasets import LoadImages, LoadStreams
from yolov5.utils.general import LOGGER, check_img_size, non_max_suppression, scale_coords, check_imshow, xyxy2xywh
from yolov5.utils.torch_utils import select_device, time_sync
from yolov5.utils.plots import Annotator, colors
from deep_sort_pytorch.utils.parser import get_config
from deep_sort_pytorch.deep_sort import DeepSort
Part 1: Models not found, despite models being the parent folder.
python3 dashboard.py
Traceback (most recent call last):
File "/home/aevas/Desktop/Dashboard_2021_12/dashboard.py", line 25, in <module>
from trackerThread import trackerDeepSORT
File "/home/aevas/Desktop/Dashboard_2021_12/trackerThread.py", line 15, in <module>
from yolov5.models.experimental import attempt_load
File "/home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5/models/experimental.py", line 14, in <module>
from models.common import Conv
ModuleNotFoundError: No module named 'models'
Part 2: After changing import models.common to import common, it turns out even common cannot be found despite being in the same folder !
python3 dashboard.py
Traceback (most recent call last):
File "/home/aevas/Desktop/Dashboard_2021_12/dashboard.py", line 25, in <module>
from trackerThread import trackerDeepSORT
File "/home/aevas/Desktop/Dashboard_2021_12/trackerThread.py", line 15, in <module>
from yolov5.models.experimental import attempt_load
File "/home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5/models/experimental.py", line 14, in <module>
from common import Conv
ModuleNotFoundError: No module named 'common'
This is how the files are like in the folder models
and this is how the import portion of experimental.py looks like
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
"""
Experimental modules
"""
import math
import numpy as np
import torch
import torch.nn as nn
from common import Conv
from utils.downloads import attempt_download
I have consulted the following links, but to no avail:
https://towardsdatascience.com/how-to-fix-modulenotfounderror-and-importerror-248ce5b69b1c
Python can't find module in the same folder
ModuleNotFoundError: No module named 'models'
https://github.com/ultralytics/yolov5/issues/353
I understand that I can change it to import .common and then the module can be successfully imported. However, the next line import utils causes a similar error. utils is on the same level as models. Which means there is going to be a cascade of moduleNotFoundError errors at this rate. I also understand using the folder ()method is inadvisable, hence I did not continue with it.
As I had mentioned that I had copied the entire cloned github repo over, when executed standalone, the github repo works perfectly fine. There are no differences between the experimental.py.
What could be wrong?
The problem is that the models module lives in the /home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5 folder, but when you run dashboard.py from /home/aevas/Desktop/Dashboard_2021_12/, the Python interpreter does not look inside the /home/aevas/Desktop/Dashboard_2021_12/targetTrack/yolov5 folder to find modules; it only looks as far as files/folders in /home/aevas/Desktop/Dashboard_2021_12/.
The usual way to fix this would be by installing yolov5 as a package, but it does not appear that yolov5 contains the code necessary to be installed in such a way. To work around this, you need to let Python know that it needs to look inside the yolov5 folder to find the module.
The quick-and-dirty way of doing this is to add the folder to the PYTHONPATH environment variable.
A slightly less quick-and-dirty, but still pretty ugly way of doing it is by dynamically adding the yolov5 folder to sys.path before you try and import the models module. You would do this by adding something like the following to the top of dashboard.py:
from pathlib import Path
import sys
sys.path.append(str(Path(__file__, "..", "targetTrack", "yolov5").resolve()))
i have a project with the following layout:
/src
/mypckg
__init__.py
calibration.py
_const.py
/tests
test_calibration.py
conftest.py
in my test file im importing my calibration module:
from mypkg import calibration
at the same time, my calibration module imports my _const modules, which contains all my constants:
import _const
my __init__.py file contains the following imports:
## __init__ file
from . import calibration
from . import _const
now, when i run pytest from the test folder, it finds my calibration module, but it gives me this error
...\mypckg\calibration.py:7: in <module>
import _const
E ModuleNotFoundError: No module named '_const'
my calibration module apparently cant find the _const module, but if i run python calibration.pyfrom my package directory, it runs perfectly. the problem is when i try to run a function from the calibration module inside my test file.
if i import the _const module into my calibration module the following way, my test works perfectly:
from mypckg import _const
# or from . import _const
but if i import it this way, when i run python calibration.py it gives me this error:
Traceback (most recent call last):
File "calibration.py", line 8, in <module>
from . import _const
ImportError: cannot import name '_const'
i tried to google my problem but didn't find anything similar, where the sub-modules cant be loaded from a test file. any idea why this behavior and how can fix it?
I created a python project in this format:
I tried to run my test_jabba.py by cding into the tests directory and running the program and I received this error:
Traceback (most recent call last):
File "./test_jabba.py", line 12, in <module>
from tests import testbench
ImportError: No module named tests
I read around and I realized I needed __init__.py to tell python where other packages are located.
Top portion of test_jabba.py
from tests import testbench
from utils import gopher, jsonstream
I did not add __init__.py into my logs and resources directories as they do no contain any python.
My best guess would be that poc is not in your PYTHONPATH. You can either set/extend the environment variable to contain poc, or
you can manipulate the path in your script using os.path. Your imports, in this case, will have to change accordingly:
from poc.tests import testbench
from poc.utils import gopher, jsonstream
Alternatively, you can use a relative import, to import tests and utils:
from ..tests import testbench
from ..utils import gopher, jsonstream