如何使IntelliJ识别常见的Python模块?

问题:如何使IntelliJ识别常见的Python模块?

我正在使用IntelliJ 10 IDEA Ultimate Edition。

我创建了一个新文件Test.py,并且IntelliJ已正确切换到Python解析模式。(我可以通过输入“ d”来确认这一点,它会弹出“ def”作为建议,然后正确点击选项卡会给我“ def:”)

但是,当我尝试此代码时…

import os

cwd = os.getcw <Ctrl-space>

发生两件事…

  1. 我在os下面有一个乱七八糟的下划线,将鼠标悬停在它上面给我一条“未解决的参考’os’”消息
  2. 当我期望看到“ getcwd”时,没有任何建议可以击中上面的Ctrl-space。

我以为这一定是由于我没有正确配置IntelliJ以处理python模块而导致的,但是我不知道我缺少了什么。

任何IntelliJ / Python用户能够帮助我吗?

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?


回答 0

只需创建并添加Python SDK

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

并选择Python解释器的安装路径(例如,C:\Python26在Windows和/usr/bin/python2.7Linux中)作为主路径。

相关讨论:http : //devnet.jetbrains.net/thread/286883

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


回答 1

您是否设置了python解释器构面?

打开项目结构CTRL+ ALT+ SHIFT+S

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

然后:

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

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


回答 2

如果您的Python SDK已正确配置,并且您仍然面临无法识别内置程序的问题,请尝试以下操作:

File -> Invalidate Caches/Restart

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

回答 3

这就是我要做的。(而且我可能忘记了我的问题的一个重要方面,这并不是最初设置为Python项目,而是设置为Java项目,其中包含一些python文件。)

项目设置->模块->加号按钮(添加模块)-> Python

然后,单击Python解释器旁边的“ …”按钮。

在弹出的“配置SDK”对话框中,单击“ +”按钮。选择“ Python SDK”,然后选择出现在我的查找器对话框中的默认“ Python”快捷方式

等待约5分钟。阅读一些生产力提示。:)

点击确定

等待系统重建一些索引。

万岁!代码提示又回到了我的模块!

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!


回答 4

另一个可能的解决方法(解决了我的问题)

您可能已经正确配置了环境,但是由于某种原因,环境一直中断。在这种情况下,请转到:

file > project settings > modules

部署SDK列表,并[invalid]在末尾寻找一条红线。如果找到一个,则必须重新创建一个python sdk。

您以前可以使用的SDK也可能在那里,但不是红色。删除它。

现在,您可以单击new按钮并添加您喜欢的python virtualenv。现在应该可以了。

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.


回答 5

几个步骤对我有所帮助(上面提到了其中一些步骤):

通过以下方式打开项目结构:

命令+; (Mac用户)或右键单击项目“ ->打开模块设置”

  1. 构面->+ ->Python -><您的项目> ->确定
  2. 模块->Python -><选择python解释器>
  3. 项目->Project SDK -><选择相关的SDK>
  4. SDK -><确保它是正确的>

点击OK

通过以下方式打开运行/调试配置:运行->编辑配置

  1. Python解释器-><确保它是正确的>

点击OK

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.


回答 6

这就是我解决问题的方式(我已经导入了项目,并且仅在此处显示,新创建的文件未显示那些错误):

1)Command + alt + R(在Windows中为Control

2)将出现“调试”窗口,选择文件并按向右箭头(->),然后选择“编辑”,然后按Enter(将出现“编辑配置设置”窗口)

3)在配置下,您会在底部看到错误(请选择一个带有有效python sdk的模块),因此在Python解释器中,选中“使用指定的解释器”,然后在下拉菜单中选择您的Python版本

(如果没有python,请使用以下链接https://www.jetbrains.com/help/idea/2016.3/installing-updating-and-uninstalling-repository-plugins.html下载intelliJ的python插件

4)单击应用,然后将其关闭。

宾果游戏完成了。

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.


回答 7

使用需求文件(请参阅指向JetBrains文档的链接)

就我而言,我需要arrow。所以,我加了

arrow==0.7.0

到我的项目 requirements.txt

然后Intellij提示我第一次import arrow使用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


回答 8

我的问题类似于@Toddarooski的问题,除了我在“ Dependencies”选项卡下具有的模块未列出SDK之外。我右键单击“ SDK”,从下拉菜单中选择“编辑”,然后选择我的Python SDK。做到了。

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.


回答 9

在取消选中“运行/调试配置”中的以下选项后,我可以使用它 main.py

Add content roots to PYTHONPATH

Add source roots to PYTHONPATH

这是在我使缓存无效并重新启动之后。

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.


回答 10

甚至我在Pycharm中的Intellisense都不适合时间模块,例如 系统中的问题没有选择口译员转到文件 -> 设置…(Ctrl + Alt + S)打开项目口译员

就我而言,项目口译员被选中。我选择了可​​用的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.