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
.
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... ?
ImportError: No module named … error in Python - set PYTHONPATH still not working
I have a writeup on this but I'll copy the relevant text inline.
You have two problems:
- You need to export
PYTHONPATH
(export PYTHONPATH=/full/path/to/src
) - You should run with
python -m module.path
instead ofpython path/to/file.py
The core problem is python path/to/file.py
puts path/to
on the beginning of the PYTHONPATH
(sys.path
)
This causes imports to start from (in your case) src/ml
instead of the expected src
.
By using -m
, you avoid this path munging and it will correctly keep src
as the beginning of your sys.path
.
When I test this in PyCharm, it works if set python/src to become source root, regardless what working directory is. But I cannot figure out how to set this properly when I run this from command line.
You can do this by cd
ing into the src
directory
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 when trying to run Python script
This issue arises due to the ways in which the command line IPython interpreter uses your current path vs. the way a separate process does (be it an IPython notebook, external process, etc). IPython will look for modules to import that are not only found in your sys.path, but also on your current working directory. When starting an interpreter from the command line, the current directory you're operating in is the same one you started ipython in. If you run
import os
os.getcwd()
you'll see this is true.
However, let's say you're using an ipython notebook, run os.getcwd()
and your current working directory is instead the folder in which you told the notebook to operate from in your ipython_notebook_config.py file (typically using the c.NotebookManager.notebook_dir
setting).
The solution is to provide the python interpreter with the path-to-your-module. The simplest solution is to append that path to your sys.path list. In your notebook, first try:
import sys
sys.path.append('my/path/to/module/folder')
import module_of_interest
If that doesn't work, you've got a different problem on your hands unrelated to path-to-import and you should provide more info about your problem.
The better (and more permanent) way to solve this is to set your PYTHONPATH, which provides the interpreter with additional directories look in for python packages/modules. Editing or setting the PYTHONPATH as a global var is os dependent, and is discussed in detail here for Unix or Windows.
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
Python import error: 'No module named'
You need to install that library, if you're on windows which it seems because of your "C://" first install setuptools.
https://pypi.python.org/pypi/setuptools
Using Windows 8 (which includes PowerShell 3) or earlier versions of Windows with PowerShell 3 installed, it’s possible to install with one simple Powershell command. Start up Powershell and paste this command:
(Invoke-WebRequest https://bootstrap.pypa.io/ez_setup.py).Content | python -
You must start the Powershell with Administrative privileges or you may choose to install a user-local installation:
(Invoke-WebRequest https://bootstrap.pypa.io/ez_setup.py).Content | python - --user
If you have Python 3.3 or later, you can use the py command to install to different Python versions. For example, to install to Python 3.3 if you have Python 2.7 installed:
(Invoke-WebRequest https://bootstrap.pypa.io/ez_setup.py).Content | py -3 -
Once installed run as follows:
easy_install semanticnet
Related Topics
How to Make a 4D Plot with Matplotlib Using Arbitrary Data
What Is This Odd Colon Behavior Doing
How to Use Youtube-Dl from a Python Program
Compare Two Different Files Line by Line in Python
Can Iterators Be Reset in Python
How to Open a Website in My Web Browser Using Python
Cannot Install Python 3.7 on Osx-Arm64
Python - Typeerror: 'Int' Object Is Not Iterable
Python Extending with - Using Super() Python 3 VS Python 2
Get the Name of a Pandas Dataframe
Generate a Random Derangement of a List
How Many Concurrent Requests Does a Single Flask Process Receive
How to Increment a Datetime by One Day