Python error ImportError: No module named
Based on your comments to orip's post, I guess this is what happened:
- You edited
__init__.py
on windows. - The windows editor added something non-printing, perhaps a carriage-return (end-of-line in Windows is CR/LF; in unix it is LF only), or perhaps a CTRL-Z (windows end-of-file).
- You used WinSCP to copy the file to your unix box.
- WinSCP thought: "This has something that's not basic text; I'll put a .bin extension to indicate binary data."
- The missing
__init__.py
(now called__init__.py.bin
) means python doesn't understand toolkit as a package. - You create
__init__.py
in the appropriate directory and everything works... ?
Cannot import module from source directory in pycharm
For some frigging reason i don't understand setting the src
directory as my source sources root
instead of the project_title
directory (which contains it anyways...but what do I know) worked.
So solution was:
-> settings
-> project structure
-> remove previous content root path and make content source the src
directory
import like this:
from pipeline_tools.helpers import func_1, func_2
Note: that i do not have a __init__.py
file in my folder, as I believe i read they're not necessary anymore.
From Nick Coghlan’s Python NotesPrior to Python 3.3, filesystem directories, and directories within zipfiles, had to contain an init.py in order to be recognised as Python package directories. Even if there is no initialisation code to run when the package is imported, an empty init.py file is still needed for the interpreter to find any modules or subpackages in that directory.
This has changed in Python 3.3: now any directory on sys.path with a name that matches the package name being looked for will be recognised as contributing modules and subpackages to that package.
Meet import error in terminal, but PyCharm can run it
pycharm adds your project directory to the PYTHONPATH environment variable (you could add other folders with Settings->Project Structure->Add Content Root).
Outside of the virtual environment of pycharm your project was not set to any search path.
Two options:
- Append project path to PYTHONPATH environment variable (either create new environment variable PYTHONPATH or add path with ";" to existing variable)
- use sys.path.append:
add following lines
import sys
sys.path.append(r"../project")
before import datasets
in train.py Pycharm ModuleNotFoundError: No module named // python import from child directory fails
If you look into PyCharm configuration, there are two options:
- Add content roots to PYTHONPATH
- Add source roots to PYTHONPATH
In your case, the first one allows you to run the script correctly because it adds root_project path in the PYTHONPATH environment variable.
So, if you want to run the script also in command line you should set the same variable.
You can proceed like this:
Open command line
If you are on Linux you can use
export PYTHONPATH=<absolute_path_of_root_project>
If you are on Windows you can use
SET PYTHONPATH=<absolute_path_of_root_project>
Run script
~/Desktop/root_project/subfolder » python3 sub_script.py
Python: 'ModuleNotFoundError' when trying to import module from the same package
In fact, the file games.py
and lock.py
and pointClass.py
is in the same folder so why not importing it directly like this :
from lock import RWLock
from pointClass import *
import another subpackage from a subpackage doesn't show docstirng in Pycharm
After days of research, I found the answer.
Solution 1 :
Do from src.folder2.script2
instead of from folder2.script2
because I'm opening the project from the parent directory of src
.
Solution 2 :
Set the source directory of the project as src
as it currently is the parent directory of src
.
Related Topics
Find the Date for the First Monday After a Given Date
Overriding the Save Method in Django Modelform
How to Get All the Request Headers in Django
Cartesian Product of a Dictionary of Lists
How Dangerous Is Setting Self._Class_ to Something Else
Row-Wise Average for a Subset of Columns with Missing Values
Round Up to Second Decimal Place in Python
Split Dataframe into Relatively Even Chunks According to Length
Overflowerror: Long Int Too Large to Convert to Float in Python
When to Use Get, Get_Queryset, Get_Context_Data in Django
How to Write a Generator Class
Why Do We Need to Call Zero_Grad() in Pytorch
How to Get a Gcp Bearer Token Programmatically with Python