Toddarooski
Toddarooski

Reputation: 2675

How do I get IntelliJ to recognize common Python modules?

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?

Upvotes: 222

Views: 140953

Answers (14)

Victor Sergienko
Victor Sergienko

Reputation: 13495

In 2025, on MacOS, I needed to:

  • Go to Project settings;
  • In Platform Settings - SDKs, make sure proper Python SDK exists.
    • I had to choose Homebrew Python instead of the stock /usr/local/bin/python;
  • In Platform Settings - Global Libraries, check that the SDK has a collection of paths in "Site Packages" list box. The stock Python SDK had it empty in my case.
  • In Project - SDK, check that the right SDK is selected.
  • In Project - Modules, check that your module has this SDK selected, and has interpreter library – (my problem was that the stock Python SDK didn't have it).
  • In Project - Facets, check that the right SDK is selected.

Perhaps some of these steps are redundant, but better safe than sorry.

Upvotes: 0

bsautner
bsautner

Reputation: 4822

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.

Upvotes: 2

Adel
Adel

Reputation: 181

(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

Upvotes: 3

PIYUSH
PIYUSH

Reputation: 1

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.

Upvotes: 0

user3079474
user3079474

Reputation: 1793

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.

Upvotes: 1

Eli
Eli

Reputation: 4946

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.

Upvotes: 7

Romain Vincent
Romain Vincent

Reputation: 3581

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.

Upvotes: 6

linuts
linuts

Reputation: 6748

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

Upvotes: 157

hulesh chandra
hulesh chandra

Reputation: 71

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.

Upvotes: 1

user108569
user108569

Reputation: 460

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.

Upvotes: 0

db42
db42

Reputation: 4544

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

Upvotes: 222

Kirby
Kirby

Reputation: 15865

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

Upvotes: 0

Alp
Alp

Reputation: 29739

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

Upvotes: 103

Toddarooski
Toddarooski

Reputation: 2675

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!

Upvotes: 18

Related Questions