Reputation: 763
I installed bottle on Python 3.4 with pip install. In the terminal, when I do:
$ python3.4
>>>import bottle # shows no import error
>>>
but when I do it in PyCharm, it says:
import bottle ImportError: No module named 'bottle'
Upvotes: 76
Views: 200479
Reputation: 534
It's a shame that I have to revisit this thread every once in a while to fix the same ModuleNotFound
exception over and over again since 2016... Sometimes I have a new package and PyCharm cannot locate it, sometimes we upgrade to a newer Python version and PyCharm just throws it at me again, that's quite annoying.
In general, first you would want to check if the Python interpreter is the correct one, then try to mark the directory as source root and add it to the PYTHONPATH
, remove any __pycache__
folders, invalidate cache and restart the IDE, wait for it to reindex your files or update the skeletons. Your last resort is to uninstall the package and reinstall it from the interpreter.
Recently I had this happened to me again and the way I solved it is quite interesting. I can import the module from PyCharm's built-in terminal but not in any script, and of course none of the solutions above worked. So, in case you've tried everything else with no luck, and the source root or interpreter are all correct, here's the solution you might want to try out:
import
should succeed but you still cannot run it from the debuggerI have no idea why checking and then unchecking the box would magically fix the issue, but it just works.
Upvotes: 0
Reputation: 1899
I am using Ubuntu 16.04. For me, it was the incorrect interpreter, which was by default using the virtual interpreter from the project.
So, make sure you select the correct one, as the pip install will install the package to the system Python interpreter.
Upvotes: 5
Reputation: 2514
For me, none of the above worked, and curiously even within one file some imports worked, some didn't:
from folder1.folder2.folder3.my_python_file import this_function # worked
from folder1.folder2.folder3.my_python_file import that_function # didn't work
Follow the above advice, but if it doesn't fix it additionally, (in PyCharm) click File
>> Repair IDE
and confirm all the 6 steps one after another.
Upvotes: 2
Reputation: 3995
The settings are changed for PyCharm 5+.
Settings:
Install package:
Upvotes: 11
Reputation: 503
I had the same problem, I tried all fixes like installing from the project interpreter and installing from python console, nothing worked. What worked was just going to the project folder from the terminal and installing it from there.
Upvotes: 0
Reputation: 19
In the case where you are able to import the module when using the CLI interpreter but not in PyCharm, make sure your project interpreter in PyCharm is set to an actual interpreter (eg. /usr/bin/python2.7) and not venv (~/PycharmProject/venv/...)
Upvotes: 0
Reputation: 6494
In some cases no "No module ..." can appear even on local files. In such cases you just need to mark appropriate directories as "source directories":
Upvotes: 84
Reputation: 103965
in your PyCharm project:
+
button to install additional python modulesUpvotes: 135