Python - Module Not Found
All modules in Python have to have a certain directory structure. You can find details here.
Create an empty file called __init__.py
under the model
directory, such that your directory structure would look something like that:
.
└── project
└── src
├── hello-world.py
└── model
├── __init__.py
└── order.py
Also in your hello-world.py
file change the import statement to the following:
from model.order import SellOrder
That should fix it
P.S.: If you are placing your model
directory in some other location (not in the same directory branch), you will have to modify the python path using sys.path
.
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... ?
ModuleNotFoundError' when trying to import module from imported package
FIRST, if you want to be able to access man1.py from man1test.py AND manModules.py from man1.py, you need to properly setup your files as packages and modules.
Packages are a way of structuring Python’s module namespace by using
“dotted module names”. For example, the module nameA.B
designates a
submodule namedB
in a package namedA
....
When importing the package, Python searches through the directories on
sys.path
looking for the package subdirectory.The
__init__.py
files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such asstring
, from unintentionally
hiding valid modules that occur later on the module search path.
You need to set it up to something like this:
man
|- __init__.py
|- Mans
|- __init__.py
|- man1.py
|- MansTest
|- __init.__.py
|- SoftLib
|- Soft
|- __init__.py
|- SoftWork
|- __init__.py
|- manModules.py
|- Unittests
|- __init__.py
|- man1test.py
SECOND, for the "ModuleNotFoundError: No module named 'Soft'
" error caused by from ...Mans import man1
in man1test.py, the documented solution to that is to add man1.py to sys.path
since Mans is outside the MansTest package. See The Module Search Path from the Python documentation. But if you don't want to modify sys.path
directly, you can also modify PYTHONPATH
:
sys.path
is initialized from these locations:
- The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH
(a list of directory names, with the same syntax as the shell variablePATH
).- The installation-dependent default.
THIRD, for from ...MansTest.SoftLib import Soft
which you said "was to facilitate the aforementioned import statement in man1.py", that's now how imports work. If you want to import Soft.SoftLib in man1.py, you have to setup man1.py to find Soft.SoftLib and import it there directly.
With that said, here's how I got it to work.
man1.py:
from Soft.SoftWork.manModules import *
# no change to import statement but need to add Soft to PYTHONPATH
def foo():
print("called foo in man1.py")
print("foo call module1 from manModules: " + module1())
man1test.py
# no need for "from ...MansTest.SoftLib import Soft" to facilitate importing..
from ...Mans import man1
man1.foo()
manModules.py
def module1():
return "module1 in manModules"
Terminal output:
$ python3 -m man.MansTest.Unittests.man1test
Traceback (most recent call last):
...
from ...Mans import man1
File "/temp/man/Mans/man1.py", line 2, in <module>
from Soft.SoftWork.manModules import *
ModuleNotFoundError: No module named 'Soft'
$ PYTHONPATH=$PYTHONPATH:/temp/man/MansTest/SoftLib
$ export PYTHONPATH
$ echo $PYTHONPATH
:/temp/man/MansTest/SoftLib
$ python3 -m man.MansTest.Unittests.man1test
called foo in man1.py
foo called module1 from manModules: module1 in manModules
As a suggestion, maybe re-think the purpose of those SoftLib files. Is it some sort of "bridge" between man1.py and man1test.py? The way your files are setup right now, I don't think it's going to work as you expect it to be. Also, it's a bit confusing for the code-under-test (man1.py) to be importing stuff from under the test folder (MansTest).
working with packages gets a module not found error
You can take a look here for an explanation of absolute VS relative imports.
Anyway, you can either use a relative import:
module1.py
from .module2 import func2
(using the dot in front of module2 tells the program to look for module2 in the same folder).
Or, you can use an absolute import, i.e. by giving the full path of the file you are importing from the project’s root folder.
In your case, it would be:
module1.py
from my_packgs.module2 import func2
If you're using an IDE like PyCharm, you'll notice that in the case of absolute import, the instruction is marked as an error.
This is because the IDE doesn't know that the absolute path you provide in module1.py is using the folder where main.py is located as starting point (since you're executing main.py, the path where main.py is located is the project root folder). Anyway, when you run main.py, everything works.
Python module not found even though module was created
You can try the sys.path.append
method. Whatever modules you want to import, find the path to those modules and pass it to the function.
Example:
If my current working directory is /home/user_name/Desktop/Scripts/Main.py
and I want to import some file Factorial.py
which is at /home/user_name/Documents/OtherScripts/
, I can do the following
# Inside your Main.py file
import sys
sys.path.append("/home/user_name/Documents/OtherScripts/")
from Factorial import *
Unable to import a module that is definitely installed
In my case, it is permission problem. The package was somehow installed with root rw permission only, other user just cannot rw to it!
Module not found error in Visual Studio Code despite the fact that I have just installed it
Have you tried to install cherrypy
using C:\Users\username\AppData\Local\Programs\Python\Python37-32\Scripts\pip install cherrypy
When I install py libraries I always run where python
and find the right python copy it and modify the path so it goes in Scripts
then run pip install cherrypy
Also I noticed that when I install py libraries it seems VSCode won't recognize it even though it runs. To fix this you have to restart VSCode again
Another possibility is that your VSCode isn't using correct version of Python. To check this look at VSCode bottom left you should see your python version. If VSCode can't recognize it you will have to enter the Python path manually by Open Command Palette Ctrl+Shift+P
and choose Python: Select Interpreter
then click enter Interpreter path
Related Topics
How to Bind the Enter Key to a Function in Tkinter
Importerror: No Module Named Win32Com.Client
Multiple Ping Script in Python
How to Look Ahead One Element (Peek) in a Python Generator
What Is the Most Compatible Way to Install Python Modules on a MAC
Using Cprofile Results with Kcachegrind
Different Meanings of Brackets in Python
Converting "Yield From" Statement to Python 2.7 Code
Extract Email Sub-Strings from Large Document
Pandas Filling Missing Dates and Values Within Group
Remove Reverse Duplicates from Dataframe
Equivalent of Numpy.Argsort() in Basic Python
Replace Nth Occurrence of Substring in String
Delete File from Zipfile with the Zipfile Module
Calculating Pearson Correlation and Significance in Python
Regular Expression Usage in Glob.Glob
How to Iterate Through Dictionary in a Dictionary in Django Template