How do I get IntelliJ to recognize common Python modules?

PythonIntellij Idea

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

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!

Solution 5 - Python

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.

Solution 6 - Python

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.

Solution 7 - Python

(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

Solution 8 - Python

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

  1. Click on apply then close it.

Bingo it's done.

Solution 9 - Python

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.

Solution 10 - Python

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

Solution 11 - Python

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.

Solution 12 - Python

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.

Solution 13 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionToddarooskiView Question on Stackoverflow
Solution 1 - Pythondb42View Answer on Stackoverflow
Solution 2 - PythonlinutsView Answer on Stackoverflow
Solution 3 - PythonAlpView Answer on Stackoverflow
Solution 4 - PythonToddarooskiView Answer on Stackoverflow
Solution 5 - PythonRomain VincentView Answer on Stackoverflow
Solution 6 - PythonEliView Answer on Stackoverflow
Solution 7 - PythonAdelView Answer on Stackoverflow
Solution 8 - Pythonhulesh chandraView Answer on Stackoverflow
Solution 9 - Pythonuser3079474View Answer on Stackoverflow
Solution 10 - PythonKirbyView Answer on Stackoverflow
Solution 11 - Pythonuser108569View Answer on Stackoverflow
Solution 12 - PythonPIYUSHView Answer on Stackoverflow
Solution 13 - PythonbsautnerView Answer on Stackoverflow