75

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'

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Tinno TL
  • 753
  • 1
  • 5
  • 7

9 Answers9

134

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

Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
  • 1
    your welcome, also note a useful trick in the Settings panel, click the gear button above the `+` button and pick "Create VirtualEnv" to create a new environment in which the modules you will install later on won't mess up your python installation system-wide. This is also useful is you want to test different versions of a same module. – Thomasleveil Sep 26 '14 at 23:50
  • 6
    "Project Interpreter" is now under Settings > Project: [your project] > Project Interpreter. Or alternatively just use the search feature with Ctrl + Shift + A. – Matthew Woo Jul 25 '17 at 23:58
  • 2
    How can I do this for a locally developed module? – chris Mar 13 '18 at 22:30
  • 1
    @ChrisAnderson, go to `Project structure` in the menu and add the folder containing that module sources to the sources – Thomasleveil Mar 13 '18 at 22:55
78

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

Ivan Talalaev
  • 6,014
  • 9
  • 40
  • 49
11

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

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Vinay Vemula
  • 3,855
  • 1
  • 21
  • 24
  • If I follow your steps until "Install the package" it seems to be that pycharm isn't able to install the package because after 10 minutes it is still not installed. That doesn't seem to be normal. – hallo97 Feb 01 '23 at 01:07
5

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

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Coder
  • 1,415
  • 2
  • 23
  • 49
5

PyCharm 2019.3, my solution is below:

enter image description here

bad_coder
  • 11,289
  • 20
  • 44
  • 72
MarsYoung
  • 471
  • 1
  • 5
  • 13
2

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.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
safex
  • 2,398
  • 17
  • 40
1

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

enter image description here

bad_coder
  • 11,289
  • 20
  • 44
  • 72
0

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/...)

Bruce Leat
  • 19
  • 1
0

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.

Hakun
  • 357
  • 3
  • 10