Unable to find module/folder despite being in the same directory - python

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()))

Related

ModuleNotFoundError when import from constants file in python

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")

ImportError and ModuleNotFoundError: how to get a script running from the command line?

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

VSCode Python test discovery not loading properly because of imports

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

I have coded to import python modules into main.py . But I have problem to import

E:\Karya\Python <-- main.py inside here
E:\Karya\packages\extra< -- iota.py inside here
import sys
sys.path.append('E:\Karya')
sys.path.append('E:\Karya\Python')
from ..packages.extra import iota
print(iota.FunI())
Question:call function FunI() by import iota.py
ERROR :
$ C:/Users/ready/AppData/Local/Microsoft/WindowsApps/python3.9.exe e:/Karya/Python/main.py
Traceback (most recent call last):
File "e:\Karya\Python\main.py", line 5, in
from ..packages.extra import iota
ImportError: attempted relative import with no known parent package
detail:
tree directory, main.py inside python folder and iota.py inside extra folder
Error
Code
Just add packages directory to sys.path and import iota from extra
sys.path.append("E:\\Karya\\packages")
from extra import iota
Make sure that there is init.py file in extra directory (and I think it is needed in packages directory too)

Packaging python files not working. "ModuleNotFoundError: No module named "Pokemon""

I'm trying to make an api for Pokemon, and I was thinking of packaging it, but no matter what I do, as soon as I try to import from this file, it comes up with this error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/student/anaconda3/lib/python3.6/site-packages/pokeapi/__init__.py", line 1, in <module>
from Pokemon import *
ModuleNotFoundError: No module named 'Pokemon'
The directory is like this:
/pokeapi
/pokeapi
__init__.py
Pokemon.py
setup.py
I install it with pip, and that error comes up.
Code for init.py:
from Pokemon import *
Code for Pokemon.py: https://hastebin.com/qegupucuma.py
I don't know what I'm doing wrong
I fixed it by going through the init.py file, and changing it to:
from .pokemon import IDfromPokemon
from .pokemon import PokemonfromID
from .pokemon import PokemonLearnset
from .pokemon import PokemonLocations
from .pokemon import PokemonTypes
from .pokemon import PokemonSprite
from .pokemon import ShinyPokemonSprite
from .pokemon import PokemonAbilities
from .pokemon import Pokemon
From everywhere else, I saw that the dots were a bad thing, but they worked this time, so I'm confused. Eh. It worked, so whatever.
You are trying to import Pokemon without accessing the parent folder. If you do not use relative imports or write the full path for the module, it will think the module you are trying to import is in the project root.
Basically, the root is the folder you are running it. Try creating a pokemon.py file into the first pokeapi folder, nexto to setup.py and see that you can import it normally.
Change your __init__.py either to: from pokeapi.pokemon import * or from . pokemon import * and then use it as pokeapi.<Pokemon module function>

Categories