218

I'm using IntelliJ 10 IDEA Ultimate Edition.

I've created a new file Test.py, and IntelliJ has correctly switched to Python parsing mode. (I can confirm this by typing "d", it pops up "def" as a suggestion, and hitting tab correctly gives me "def :")

However, when I try this code...

import os

cwd = os.getcw <Ctrl-space>

Two things happen....

  1. I get a squggly underline underneath os and hovering over it gives me a "Unresolved reference 'os'" message
  2. I get no suggestions hitting Ctrl-space above, when I'm expecting to see "getcwd".

I'm assuming that this must be a result of my not configuring IntelliJ properly in order to handle python modules, but I have no idea what it is I'm missing.

Any IntelliJ/Python users able to help me out?

nycynik
  • 7,371
  • 8
  • 62
  • 87
Toddarooski
  • 2,645
  • 2
  • 16
  • 12
  • I had the same issue, tried every answer on the internet. Turns out my 32 bit install of python was the issue. Uninstalled that and installed the latest 64 bit version and had no problems. – I am a Terrible Programmer Feb 04 '22 at 03:14
  • I had an issue where I would add a Python module in a monorepo, it would show as a module (bold text), but then when I closed the dialog it would disappear. The issue was the module was being automatically unloaded. So check `Load/Unload modules...` from Actions. – vaughan May 18 '22 at 10:21

13 Answers13

217

Just create and add Python SDK

File -> Project Structure -> Project -> Project SDK -> new

and select the installation path of your Python interpreter (for example, C:\Python26 in windows and /usr/bin/python2.7 in Linux) as the home path.

Related discussion: http://devnet.jetbrains.net/thread/286883

db42
  • 4,474
  • 4
  • 32
  • 36
152

Have you set up a python interpreter facet?

Open Project Structure CTRL+ALT+SHIFT+S

Project settings -> Facets -> expand Python click on child -> Python Interpreter

Then:

Project settings -> Modules -> Expand module -> Python -> Dependencies -> select Python module SDK

Andrea Bergonzo
  • 3,983
  • 4
  • 19
  • 31
linuts
  • 6,608
  • 4
  • 35
  • 37
  • 1
    v12.1.4 Doesn't have the Project Settings > Facets menu option, but @db42's solution worked a treat – Trindaz Jun 25 '13 at 23:41
  • I have created a facet, and it did not work, your answer was the solution!! wonderfull! :) – Rodislav Moldovan Oct 23 '14 at 10:06
  • Odd. I don't know what a facet is so I looked it up and the below page does not show python as an available facet. https://www.jetbrains.com/idea/help/available-facets-and-their-dependencies.html – netskink Apr 17 '15 at 22:51
  • 1
    IntelliJ IDEA 14 Ultimate has this feature, and it worked perfectly without breaking my Java module detection like db42's answer did. – Patrick M Oct 12 '15 at 15:30
  • after doing this it is not even showing suggestions to built in modules to which previously it was there – Bharat Aug 29 '17 at 13:43
  • I had fixed mine by doing this step and then invalidating cache as per the answer below by @Alp – mel3kings Apr 17 '18 at 06:36
  • 1
    This is the correct answer. Notice that if your module has separate `_main` and `_test` sub-modules, then you will need to add the Python facet to those as well. – Jonathan E. Landrum Apr 09 '19 at 14:29
  • How do I include 2 python-versions in one project? – Gerd May 17 '19 at 14:40
  • If the options show _No Interpreter_ then ensure Python is installed on your machine and configure in IntelliJ under the SDK option on the same open dialog - see [Configure system interpreters](https://www.jetbrains.com/help/idea/configuring-local-python-interpreters.html) – boardtc Aug 06 '19 at 13:35
  • Adding the interpreter to the facet was the solution for me – Tarol Apr 15 '20 at 10:11
96

If your Python SDK is properly configured and you are still facing the problem that builtins are not recognized, try this:

File -> Invalidate Caches/Restart
Alp
  • 29,274
  • 27
  • 120
  • 198
19

Here's what I had to do. (And I probably forgot an important aspect of my problem, which is that this wasn't set up as a Python project originally, but a Java project, with some python files in them.)

Project Settings -> Modules -> Plus button (add a module) -> Python

Then, click the "..." button next to Python Interpreter.

In the "Configure SDK" dialog that pops up, click the "+" button. Select "Python SDK", then select the default "Python" shortcut that appears in my finder dialog

Wait about 5 minutes. Read some productivity tips. :)

Click Ok

Wait for the system to rebuild some indexes.

Hooray! Code hinting is back for my modules!

Toddarooski
  • 2,645
  • 2
  • 16
  • 12
7

Few steps that helped me (some of them are mentioned above):

Open project structure by:

command + ; (mac users) OR right click on the project -> Open Module Settings

  1. Facets -> + -> Python -> <your-project> -> OK
  2. Modules -> Python -> <select python interpreter>
  3. Project -> Project SDK -> <select relevant SDK>
  4. SDKs -> <make sure it's the right one>

Click OK.

Open Run/Debug Configurations by: Run -> Edit Configurations

  1. Python Interpreter -> <make sure it's the right one>

Click OK.

Eli
  • 4,576
  • 1
  • 27
  • 39
6

Another possible fix (solved my problem)

You might have configured the environment properly but for some reason it broke along the way. In this case go to:

file > project settings > modules

Deploy the list of SDKs and look for a red line with [invalid] at the end. If you find one, you have to recreate a python sdk.

It is likely that your previously working SDK is there too, but not red. Delete it.

Now you can click on the new button and add your favorite python virtualenv. And it should work now.

Romain Vincent
  • 2,875
  • 2
  • 22
  • 29
2

(solved my problem) File -> Project structures -> Modules -> Add (small plus sign) -> Import Module -> Add the path contains the files (e.g. src/mymodule) -> Create Module from existing sources -> Next -> next -> Finish. You should see a file with .iml in the directory where you cannot import; that should do the trick

Adel
  • 161
  • 6
1

This is how i solved my problem (i have imported the project and it was showing there only, newly created files were not showing those errors):

1) Command + alt + R (Control in case of windows

2) Debug window will appear, select your file and press right arrow (->) and choose Edit then press enter (Edit configuration setting window will appear)

3) Under configuration, at the bottom you can see the error (please select a module with a valid python sdk), So in Python Interpreter, check Use Specified Interpreter, then in drop down you select your Python version

(In case python is not there download python plugin for intelliJ using following link https://www.jetbrains.com/help/idea/2016.3/installing-updating-and-uninstalling-repository-plugins.html

4) Click on apply then close it.

Bingo it's done.

1

I got it to work after I unchecked the following options in the Run/Debug Configurations for main.py

Add content roots to PYTHONPATH

Add source roots to PYTHONPATH

This is after I had invalidated the cache and restarted.

user3079474
  • 1,653
  • 2
  • 24
  • 37
1

resurrecting an old thread for 2022 - in my case I had to create a local python interpreter facet since i was using a remote one over SSH I'd get red squiggles even over import time even though it'd run on the remote system (A raspberry pi).

Some answers here work and say to make a facet but my case was different since i had one, just one that didn't work locally.

bsautner
  • 4,479
  • 1
  • 36
  • 50
0

Use a Requirements File (see link to JetBrains documentation)

In my case I needed arrow. So, I added

arrow==0.7.0

to my projects requirements.txt

Then Intellij prompted me to add the library the first time that I wrote import arrow in a Python script

Kirby
  • 15,127
  • 10
  • 89
  • 104
0

My problem was similar to @Toddarooski 's, except that the module I had, under the "Dependencies" tab, had no SDK listed. I right clicked on 'SDK', picked edit from the drop down menu, and selected my Python SDK. That did the trick.

user108569
  • 450
  • 5
  • 8
0

Even my Intellisense in Pycharm was not working for modules like time Problem in my system was no Interpreter was selected Go to File --> Settings... (Ctrl+Alt+S) Open Project Interpreter

Project Interpreter In my case was selected. I selected the available python interpreter. If not available you can add a new interpreter.

PIYUSH
  • 1