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

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 !

Related

How to mark src folder as Sources Root through linux terminal?

I am trying to run a python file from terminal. It has a bunch of imports from modules I wrote, and those modules also import from other modules from throughout my project. As PyCharm was my main editor, I could mark the src directory as Sources Root, which allowed me to import other modules in two ways. Say I have a module shared, I could import from it as:
from src.shared.timer import Timer
But also as:
from shared.timer import Timer
When I run my script main.py from within pycharm everything runs perfectly
The problem is that when I run main.py from command line, my imports give error "No module named shared" or "No module named src" depending on whether I call from the top-level directory or from the src directory. Either way, I can never get all imports right. Is there a way to mark my src directory as Sources Root, similar to in PyCharm? I want to avoid refactoring everything and then finding out there is some other issue.
from top level directory in repository, I tried to run
python3.8 -m src.main.py
and got
ModuleNotFoundError: No module named 'shared'

VSCode Python error in importing modules from subdirectories

My project file structure is like this,
project/src/test/myscript.py
project/src/utils/file_utils.py
When I run myscript.py, which has from utils import file_utils, it gave me error:
ModuleNotFoundError: No module named 'utils'
Previously in Pycharm IDE I did not get this type of error (maybe due to _ init _.py), the subdirs of the same parent dir could be detected. But not sure for VSCode, is there something I need to add for specifying the file structure? And I opened the folder project as my VSCode workspace (not sure if where I open the workspace matters)
I tried adding:
in the /project/.vscode/launch.json
"cwd": "${workspaceFolder}/src"
or in the begining of myscript.py
import sys
import os
src_path = os.path.dirname(os.path.abspath('/project/src/'))
sys.path.insert(0, src_path)
But none of them works. Does anyone have any insights? Thank you very much!
You could consider placing a .env file at the root of your project which adds your source directory to PYTHONPATH. i.e. something like
>>> cat /project/.env
PYTHONPATH=/project/src/
>>>
Your code will look a smidgen nicer without the explicit manipulation of sys.path.
VSCode's usage of .env files is documented here.
Yes, in Pycharm you didn't get this error because it adds __init__.py file automatically when you create a python module. The python identifies the structure of your project through these files, if your folder does not have __init__.py python will understand it as just any folder.
Unlike pycharm, vscode uses the workspace as the root directory to retrieve files. The first method you try is to write it in the launch.json file, which is applicable to debug rather than running it directly. You can use the following code to import:
from src.utils import file_utils

How to resolve Python Module Not Found Error?

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.

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

Unable to import Python module in Python script

I have a Python project having the following hierarchy:
- product_recommender_sys
- data
- dataset.csv
- public
- __init__.py
- startup.py
- src
- __init__.py
- recommender.py
I am trying to import the recommender.py module in startup.py.
Following is the code:
import sys
sys.path.insert(0, '/home/user1/product_recommender_sys/src')
print sys.path
from product_recommender_sys.src import recommender
recommender.recommend()
I have included __init__.py file and added the respective folders to sys.path. Also, the same import statement works perfectly fine on the Python interpreter, but fails inside the script. How can I get the import to work inside the script?
I found the issue.
The startup.py module was not getting compiled since it was not being imported.
First compiling the script solves the problem.
python -m compileall product_recommender_sys/public/startup.py

Categories

Resources