How to resolve Python Module Not Found Error? - python

I am facing a problem in importing modules in python. I looked for a solution and found this. but this did not worked either.
My Directory is as follows
->MyScrapper --->MyScrapper ----->db_connection.py --->Video_Scrapper -----> video_scrapper.py --->Blogs_Scrapper -----> blogs_scrapper.py
I want to import db_connection.py in video_scrapper.py as well as blogs_scrapper.py. I have tried to import it as from MyScrapper.db_connection import DBConnection. It throws a ModuleNotFoundError: No Module named 'MyScrapper'. I have also tried using from db_connection import DBConnection and import DBConnection but none of them worked.
Please Help!!

When importing a module in python, it will search the module in this order:
build-in module
script in the current directory
PATHPYTHON
installation-dependent default
So, you need to add the MyScrapper module to the search path. If you use a virtual environment, you can add your project root directory into the PYTHONPATH, by modify the PYTHONPATH in the Scripts\activate.bat file, like this:
set PYTHONPATH=%VIRTUAL_ENV%\src;%PYTHONPATH%
I use the virtual environment, and my project struct is:
|--Include
|--Lib
|--Scripts
|--src
My source files are in the src directory.

Related

Problems with module path in Python (ModuleNotFoundError: No module named)

Today I have a problem with module in Python.
My file structure:
- library
--- Storage.py
- scripts
--- run.py
and run.py code:
import library.Storage as Storage
but I run this in PyCharm it work fine but if I run in terminal
python3 scripts/run.py
it return
import library.Storage as Storage
ModuleNotFoundError: No module named 'library'
I had tried this
fpath = os.path.dirname(__file__)
sys.path.append(fpath)
print(sys.path)
['/opt/homebrew/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/opt/homebrew/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/opt/homebrew/Cellar/python#3.9/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/opt/homebrew/lib/python3.9/site-packages', '/Users/binhot/PycharmProjects/MyProject/']
but the problems still happen
The problem is that python is trying to import the module library from inside the scripts folder. What you need to do is make a relative import. See more here: https://realpython.com/absolute-vs-relative-python-imports/#relative-imports
I had solve this problems by create setup.shto add current path to PYTHONPATH
export PYTHONPATH="${PYTHONPATH}:`pwd`"
now it work fine !

how to import module from same level directory in python

as the question, i have my directory map like this
the formatOutput contain some function for better print
now, i want use a function in module printChecked.py in package formatOutput from findInfoSystem.py
i have tried create __init__.py in all folder to treat python it is a package (the advice i get from previous answer other post) but it always failed.
case 1: from formatOutput.printChecked import print_checked_box
error is: ModuleNotFoundError: No module named 'formatOutput'
case 2: from dttn.formatOutput.printChecked import print_checked_box
error is ModuleNotFoundError: No module named 'dttn'
case 3: from ..formatOutput.printChecked import print_checked_box
error is ImportError: attempted relative import with no known parent package
i don't want to use the sys.path method because i think it is not the good way to solve problem.
Help me please !
There are two classes of solutions to this problem:
modify the python import path
use the various setup tools or manually install .egg-links or symbolic links
to "install" your development modules in the python/lib/site-packages
The first approach is simple, but the second involves delving deeper
into a number of related topics. You'd probably want a virtual environment,
and a setup.py, etc.
So for the quick and dirty, two ways to modify the python import path:
set the PYTHONPATH= environment variable to include the parent directory of your packages
in findInfoSystem before importing formatOutput.printChecked, add this:
import sys
sys.path.append("..")
i found something very weird. I'm not import direct in module file but import to init.py file of package and then import to module file from packages name and it work. it like that:
from __init__.py of systemInfo package:
import sys,os
from formatOutput import prettyAnnounce,prettyParseData
current_directory = os.getcwd()
# sys.path.insert(1, current_directory+"/formatOutput/")
# sys.path.insert(1,current_directory+"/")
then in findInfoSystem.py module, im imported again like this:
from systemInfo import current_directory, prettyAnnounce as prCh , prettyParseData as prPD
yeah, now it's work like a charm. :))) i even not sure how

ModuleNotFoundError: No module named 'libs.strings'

This is the error I am facing right, I google it a lot but solutions did not work, I am using python 3.7 in my django application.
Code:
from libs.strings import *
Error:
ModuleNotFoundError: No module named 'libs.strings'
As I have checked the libs module from pypi. The libs module doesn't seem to have strings. So doing pip install libs wont solve your problem.
Maybe libs could be a directory in your project you are trying to access. If so, then:
Check Proper path for libs folder.
In order to define libs as a single package, add empty __init__.py file in each folder; so the whole directory system act as a python module.

Can't import own Python module

I am currently running on python 3.6 on anaconda. I have a project structure where (test/lib/yolo/yolo_model.py) and (test/car/detection/cpu_yolo_detector.py).
I run my main from the test directory. My main now calls the script cpu_yolo_detector.py from withing (test/car/detection).
From cpu_yolo_detector.py I want to access the yolo_model.py with
"from lib.yolo.yolo_model import YoloModel"
but I get "no module named lib.yolo".
At the beginning of the main.py I add ('C:\\Users\\Name\\Desktop\\test\\lib\\yolo') to the sys.path and I still get that Error.
I tried both python 3.6 and 3.7 aswell as a virtual environment and without a virtual environment. If I run it with PyCharm everything seems to work but from the terminal it doesn't.
It appears test/ is the root of your project structure. If you want
from lib.yolo.yolo_model import YoloModel
to work, then the directory containing lib/ must be in sys.path.
Try adding 'C:\\Users\\Name\\Desktop\\test' to sys.path.
Try to put the two files (module and main file) in the same directory. If the module name is helpermodule
on main write:
import helpermodule
#or import a specific class/method you might need

Import collection of modules in python

I trying to import a package that I had created and when i tried to import it,
the following output is shown:
(ModuleNotFoundError: No module named 'folder1')
in each folder in my package (__init__.py) has been created.
the package is in the following path
(/Users/user/Desktop/package/folder1/test.py)
python is in the following path(/Users/user/anaconda3/lib/python3.6'
how can I import the package? do I need to change package's location?
thank you for help
Try adding your package to the python path:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
When you import a python module using import python interpreter searches in current directory & paths set in PYTHONPATH environment variable.
so inorder to work what you are expecting either you should be running the script insider the parent directory or you must include in PYTHONPATH environment variable to you folder path.

Categories

Resources