Tinno TL
Tinno TL

Reputation: 763

ImportError: No module named 'bottle' in PyCharm

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

Answers (10)

neo-mashiro
neo-mashiro

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:

  • Right click on your starting script, select "Edit Run Configuration..."
  • Under the "Configuration" tab, check the box "Run with Python Console"
  • Right click again to run your script, the import should succeed but you still cannot run it from the debugger
  • Go back to the "Configuration" tab, uncheck the box you previously checked
  • Now try running the script from the debugger, PyCharm should be able to find your modules

I have no idea why checking and then unchecking the box would magically fix the issue, but it just works.

Upvotes: 0

Coder
Coder

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.

IDE settings

Upvotes: 5

MarsYoung
MarsYoung

Reputation: 471

PyCharm 2019.3, my solution is below:

enter image description here

Upvotes: 5

safex
safex

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

Vinay Vemula
Vinay Vemula

Reputation: 3995

The settings are changed for PyCharm 5+.

  • Go to File > Default Settings
  • In left sidebar, click Default Project > Project Interpreter
  • At bottom of window, click + to install or - to uninstall.
  • If we click +, a new window opens where we can decrease the results by entering the package name/keyword.
  • Install the package.
  • Go to File > Invalidate caches/restart and click Invalidate and Restart to apply changes and restart PyCharm.

Settings:

Settings

Install package:

Install package

Upvotes: 11

ibrahim mert
ibrahim mert

Reputation: 41

I had virtual env site package problem and this solved it:

enter image description here

Upvotes: 1

Hakun
Hakun

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

Bruce Leat
Bruce Leat

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

Ivan Talalaev
Ivan Talalaev

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":

Mark as source lib directory

Upvotes: 84

Thomasleveil
Thomasleveil

Reputation: 103965

in your PyCharm project:

  • press Ctrl+Alt+s to open the settings
  • on the left column, select Project Interpreter
  • on the top right there is a list of python binaries found on your system, pick the right one
  • eventually click the + button to install additional python modules
  • validate

enter image description here

Upvotes: 135

Related Questions