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... ?
How to fix ImportError: No module named ... error in Python?
Python does not add the current directory to sys.path
, but rather the directory that the script is in. Add /home/bodacydo/work/project
to either sys.path
or $PYTHONPATH
.
ImportError: No module named something
Your package structure is OK. Your import statement is OK. The only thing missing is for the package to be visible in sys.path
, a list of locations where import statements can be resolved.
Usually we do this by "installing" the package locally with pip
, which copies your code into site-packages†. This directory is one of the entries in sys.path
, so when your code is installed in site-packages, the import statements can now be resolved as usual.
However, to install your code you'll need an installer (setup.py
script) or a build system (pyproject.toml
file) defined for the package. Your project doesn't appear to have any installer or build system, so you'll need to create one (see the Python Packaging User Guide for details about that) and then install the package with pip
. If you don't want to learn Python packaging just yet, you'll need to find another way around.
It is possible to modify sys.path
directly in main.py
, which is subsequently enabling the statement import ankur.ankur1.util
to be resolved. This is hacky and I recommend against that. It would add the restriction that executing main.py
is the only entry point to the rest of the package, and so any other code wanting to import ankur
will first need to know the path to main.py
on the filesystem. That's a messy approach and should be avoided.
Another way around is to use the environment - there is an environment variable PYTHONPATH
which can be used to augment the default search path for module files. In your shell:
export PYTHONPATH=/path/to/parent # linux/macOS
SET PYTHONPATH=C:/path/to/parent # Windows
Where parent is the directory containing ankur
subdirectory.
† The exact location of site-packages depends on your OS/platform, but you can check with import sysconfig; sysconfig.get_paths()["purelib"]
Relative imports - ModuleNotFoundError: No module named x
As was stated in the comments to the original post, this seemed to be an issue with the python interpreter I was using for whatever reason, and not something wrong with the python scripts. I switched over from the WinPython bundle to the official python 3.6 from python.org and it worked just fine. thanks for the help everyone :)
ImportError: No module named
The issue is related, as you say, to paths... When you execute your code, the interpreter has a list of places to look for the things you are importing. In this case, it doesn't know about your project's root directory.
To solve the problem, set the environment variable PYTHONPATH to your project's root directory. Something like this:
export PYTHONPATH=~/testapp
import error: 'No module named' *does* exist
My usual trick is to simply print sys.path
in the actual context where the import problem happens. In your case it'd seem that the place for the print is in /home/hughdbrown/.local/bin/pserve
. Then check dirs & files in the places that path shows..
You do that by first having:
import sys
in python 3 with the print function:
print(sys.path)
or in python 2 with print expression:
print sys.path
Import error, No module named xxxx
The behavior you are seeing can be caused if there is a module (foo.py
) or package (foo/__init__.py
) in your current directory that has a conflicting name.
In your case, I suspect there is a file named prediction.py
, and you're getting that instead of the prediction
package in your examples
directory.
Related Topics
How to Access a Standard-Library Module in Python When There Is a Local Module with the Same Name
Take Multiple Lists into Dataframe
Python: Urllib2 How to Send Cookie with Urlopen Request
Django What Is Reverse Relationship
Pandas Unique Values Multiple Columns
Regex: Attributeerror: 'Nonetype' Object Has No Attribute 'Groups'
Pyinstaller Is Not Recognized as Internal or External Command
Managing Dynamic Plotting in Matplotlib Animation Module
Find Substring in String But Only If Whole Words
How to Change the Host and Port That the Flask Command Uses
MySQL "Incorrect String Value" Error When Save Unicode String in Django
How to Treat Python Argparse.Namespace() as a Dictionary
Is There an Expression for an Infinite Iterator