How to change the root path of a python project - python

I am using MacOS with Pycharm.
My directory structure:
Inside test2_main.py, I run
from test1 import x
ModuleNotFoundError error
from test1 import x
ModuleNotFoundError: No module named 'test1'
How can I solve it if I want to use the same import code?

Go to Run->Edit Configurations and in the menu of your Run Configuration there is a field called Working 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 !

Python not finding own directory, "ModuleNotFoundError: No module named ..." (Spyder IDE)

I'm currently trying to run an old github project and am running into an error with Python 3 in Spyder. I have
from nmap_visualizer.db import Savednmap, User, db
in my code and am getting a "ModuleNotFoundError: No module named 'nmap_visualizer'"
However, the folder that contains db.py is called 'nmap_visualizer. This is my working directory and the file I'm trying to run this code in 'nmap.py' is in the same folder alongside db.py. In PYTHONPATH manager I've selected the nmap_visualizer folder for my path. In 'Preferences' under 'Run' I have 'Default working directory is:' 'The directory of the file being executed'.
When I try from .db import Savednmap, User, db I get the error "ModuleNotFoundError: No module named 'main.db'; 'main' is not a package"
I do have an init.py file which runs and imports db perfectly fine with from . import db, but I'm not sure why my 'nmap.py' file is failing.
The following helped for me: adding the directory to pythonpath
import sys
print(sys.path)
sys.path.append("\\folder with script")

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

VSCode Unable to import 'example' pylint(import-error)

I am getting pylint errors in VSCode that say they are unable to import local files. However, I am able to run the files through the debugger with no problem. I thought that pylint used the same PYTHONPATH that the interpreter uses, so I don't know why this is happening.
I have my code set up like so:
dir0
-dir1
--__init__.py
--src
---__init__.py
---srcdir1
----__init__.py
----file1.py
---srcdir2
----__init__.py
----file2.py
file1.py looks like this:
def func1():
return 1
file2.py looks like this:
from srcdir1.file1 import func1
func1()
in launch.json I have:
"env": {"PYTHONPATH": "/full/path/to/dir0/dir1/src:/usr/local/bin/python"}
Pylint is giving me an import error around "from srcdir1.file1". When I go into the debugger and click run debugger, the file runs with no issues. However, if I right click and select Run Code, I get import errors that match the pylint errors.
EDIT:
I created a file in my workspace folder called .env in my workspace folder. It is as follows:
PYTHONPATH=/Library/Python/2.7/site-packages:/Users/user/path/dir0/dir1/src:/Users/user/path/client/src:/Users/user/path/product/src
Interestingly, I can import from product (the third in the list) but not from client. Is there somewhere that this environment is being overridden?
I also have the following in the file:
import os
import shutil
import sys
For some reason, import sys (but not the others) gives me the following error: unresolved import 'sys'Python(unresolved-import)
Do you have __init__.py files inside those folders? Otherwise python won't recognise them as modules and will be unable to import the code. Have a look at https://stackoverflow.com/a/448279/5015356 for more information
The problem is that you specified a PYTHONPATH for the debugger and not the general extension to send to Pylint. Try setting PYTHONPATH in a .env environment variable definition file.

ImportError on console but not in PyCharm

I have the folowing dir structure
|population_model
--|__init__.py
--|run.py
inside __init__.py I have the following:
def my_func():
...
return
on run.py I have
from population_model import my_func
When I run the project from inside PyCharmthe code runs beautifully.
But when running from Terminal:
my/path/to/population_model/python run.py
I get ImportError: No module named population_model
What might be the cause?
Most likely PyCharm's Run configuration is setting the Working Directory to your project, where it can find population_model. In the terminal, cd to the directory first.

Categories

Resources