Python PyCharm 无法识别在开发模式下安装的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26193365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:12:17  来源:igfitidea点击:

PyCharm does not recognize modules installed in development mode

pythonpython-3.xpycharmsetuptools

提问by zegkljan

I have two pure python projects in PyCharm 3.4.1 Professional Edition. The first one, let's call it p(like package), is structured as a setuptools package (i.e. with setup.py, all requirements etc., however it is not uploaded to pypi or any other online repository). The second one, let's call it s(like script), is just a python script along with two modules.

我在 PyCharm 3.4.1 专业版中有两个纯 python 项目。第一个,我们称之为p(如包),结构为 setuptools 包(即带有 setup.py、所有要求等,但它不会上传到 pypi 或任何其他在线存储库)。第二个,我们称之为s(如脚本),只是一个带有两个模块的 python 脚本。

Project sis (in PyCharm) configured to use a dedicated virtualenv, let's call it venv.

项目s(在 PyCharm 中)配置为使用专用的 virtualenv,我们称之为venv.

The problem I have is the following: when I install the project (package) pin venvlike this:

我遇到的问题如下:当我像这样安装项目(包)pvenv

$ source /path/to/venv/bin/activate
(venv)$ cd /path/to/p
(venv)$ python3 setup.py develop

in PyCharm in project s, import pstatements are errorneous with message No module named p. However, when I run the script in s, everything is fine, the only problem is the PyCharm IDE complaining about not being able to find the module. I can live with this but it is very annoying...

在项目中的 PyCharm 中simport p语句错误并显示消息No module named p。但是,当我在 中运行脚本时s,一切都很好,唯一的问题是 PyCharm IDE 抱怨找不到模块。我可以忍受这个,但它很烦人......

Why does this happen? Is it a PyCharm thing or packaging related thing?See NEWSbelow.

为什么会发生这种情况?它是 PyCharm 的东西还是与包装相关的东西?请参阅下面的新闻



The project/package phas the following structure:

项目/包p具有以下结构:

p/
|
+- p/
|  |
|  +- __init__.py
|  +- other subpackages, modules, etc.
+- setup.py
+- README, DESCRIPTION, setup.cfg, etc.

When I configure the PyCharm project pto live in its own virtualenv and install it there in development mode, everything works fine.

当我将 PyCharm 项目配置p为存在于它自己的 virtualenv 中并在开发模式下将其安装在那里时,一切正常。



NEWS

消息

This problem is still present in PyCharm 5.0.4. However, I managed to solve it, kind-of.

这个问题在 PyCharm 5.0.4 中仍然存在。但是,我设法解决了它,有点。

For some reasons I had to install another package from pypi. I did it through PyCharm by going to File -> Settings -> Project: -> Project Interpreter, there clicking on the green +, finding the package and pressing the Install Package button. After the installation, the package installed by python3 setup.py developis well recognized by PyCharm. Obviously the problem was that PyCharm didn't have some cache in sync with reality.

由于某些原因,我不得不从 pypi 安装另一个包。我通过 PyCharm 通过转到 File -> Settings -> Project: -> Project Interpreter,点击绿色+,找到包并按下 Install Package 按钮。安装后,安装的包python3 setup.py develop被PyCharm很好的识别了。显然,问题在于 PyCharm 没有一些与现实同步的缓存。

So the new question is, can PyCharm be told to update its caches regarding the used python environment?

所以新的问题是,是否可以告诉 PyCharm 更新其关于使用的 Python 环境的缓存?

采纳答案by zegkljan

As of Pycharm 2016.1.4 (professional edition), it seems that opening a different project (e.g. through Open Recent) and then opening the original one makes Pycharm reindex the installed packages and hence recognize the develop-installed packages.

从 Pycharm 2016.1.4(专业版)开始,似乎打开一个不同的项目(例如通过 Open 最近)然后打开原始项目会使 Pycharm 重新索引已安装的包,从而识别开发安装的包。

This is enough for me.

这对我来说已经足够了。

回答by mike

In the project pane, the directory which holds p needs to be marked as source. Available under: Right-click -> "Mark Directory As" -> "Sources Root".

在项目窗格中,需要将保存 p 的目录标记为源。可用于:右键单击 ->“将目录标记为”->“源根目录”。

Note: This setting will also add it to PYTHONPATH during execution from the run menu if the corresponding check box for the option is checked in "Edit Configurations" (default).

注意:如果在“编辑配置”(默认)中选中了该选项的相应复选框,则此设置还将在从运行菜单执行期间将其添加到 PYTHONPATH。

回答by sofx

I just had same problem like yours.

我刚遇到和你一样的问题。

Seems pycharm can not recognize module installed directly by setup.py, but can recognize module installed by pip. Finally, I use pip install src_path, but I got to pip installit everytime I modify source code.

好像pycharm不能识别setup.py直接安装的模块,但是可以识别pip安装的模块。最后,我使用pip install src_path,但每次修改源代码时我都必须pip install它。

回答by Lucas van Dijk

Sort of workaround that worked for me:

对我有用的解决方法:

Open both projects in PyCharm in the same window (workspace). Now open up the settings window, and under "Project -> Project Depencies" you can now select that project sdepends on project p. Imports and autocompletion will now work fine.

在同一窗口(工作区)中打开 PyCharm 中的两个项目。现在打开设置窗口,在“Project -> Project Depencies”下,您现在可以选择该项目s取决于项目p。导入和自动完成现在可以正常工作。

回答by Hardik Gajjar

I have Just installed package using Pycharm then problem solved.

我刚刚使用 Pycharm 安装了软件包,然后问题解决了。

回答by Contango

I had a devil of a time getting PyCharm to recognize a class in a module that I had just written.

我有一段时间让 PyC​​harm 识别出我刚刚编写的模块中的一个类。

The problem is that PyCharm seems to default to not importing module class paths, which requires twoseparate fixes to correct.

问题是 PyCharm 似乎默认不导入模块类路径,这需要两个单独的修复程序才能更正。

Step 1

第1步

Right click on the module name, and mark it as "Source":

右键单击模块名称,并将其标记为“源”:

enter image description here

在此处输入图片说明

Step 2

第2步

For some reason, by default in PyCharm, it does not actually add directories marked as "Sources Root" to the Python path. Fix this by switching this on.

出于某种原因,在 PyCharm 中,默认情况下,它实际上不会将标记为“Sources Root”的目录添加到 Python 路径中。通过打开它来解决这个问题。

enter image description here

在此处输入图片说明

Extra for experts

专家额外

Notice the "Starting Script" in the image above. I assume that manually adding these lines to your Python script would also achieve the same result.

请注意上图中的“启动脚本”。我假设将这些行手动添加到您的 Python 脚本中也会达到相同的结果。

Tested On

经测试

  • Windows 10 x64.
  • PyCharm Community Edition 2017.2.3.
  • 视窗 10 x64。
  • PyCharm 社区版 2017.2.3。

回答by Shiva

The problem could be your interpreter path. Check where the interpreter is pointing to. In most cases it is ~/PycharmProjects/trials/venv/bin/python and this could be pointing to a python bath installed as part of Pycharm

问题可能是您的解释器路径。检查解释器指向的位置。在大多数情况下,它是 ~/PycharmProjects/trials/venv/bin/python 并且这可能指向作为 Pycharm 一部分安装的 python Bath

Change the softlink of python to your /usr/bin/python path and things should work fine

将 python 的软链接更改为您的 /usr/bin/python 路径,事情应该可以正常工作

回答by lego king

Solution

解决方案

  • go to settings->project interpreter.
  • click wheel framed (settings gear icon) button.
  • In the drop-down list, click Show all.... The available interpreters show up in the Project Interpreters dialog.

  • Select the desired interpreter.

  • In the toolbar of the Project Interpreters dialog box, click the button icon show paths(last option). The existing paths of the selected interpreter show up in the Interpreter Paths dialog box.

  • Add your package's path (package that you pip developed) here.ex /home/../repo-name
  • 转到settings->project interpreter
  • 单击轮框(设置齿轮图标)按钮。
  • 在下拉列表中,单击“全部显示...”。可用的解释器显示在“项目解释器”对话框中。

  • 选择所需的解释器。

  • 在项目解释器对话框的工具栏中,单击按钮图标显示路径(最后一个选项)。所选解释器的现有路径显示在“解释器路径”对话框中。

  • 在此处添加您的包路径(您 pip 开发的包).ex /home/../repo-name

voila!! you are good to go.

瞧!!你已准备好出发。

Explanation
Each project refers to an interpreter which you can find in settings->project interpreter. Now this interpreter uses a bunch of Paths to look for a library. By default it has site-packages and bunch of other paths there.
Now since you used pip develop -eor python setup.py develop, a dynamic link is created pointing to your package's repository instead of a package installation in site-packages directory. So our package's source path is not here, What we need to do is to add our source path to interpreter's Pathsto make it work

解释
每个项目都引用一个解释器,您可以在settings->project interpreter 中找到它。现在这个解释器使用一堆路径来寻找一个库。默认情况下,它有站点包和一堆其他路径。
现在,由于您使用了pip develop -epython setup.py develop,因此会创建一个指向包存储库的动态链接,而不是站点包目录中的包安装。所以我们的包的源路径不在这里,我们需要做的是将我们的源路径添加到解释器的路径中以使其工作

Ref:

参考:

  1. https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000019690-PyCharm-not-recognize-development-library-
  2. https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-reloading-interpreter-paths.html
  1. https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000019690-PyCharm-not-recognize-development-library-
  2. https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-reloading-interpreter-paths.html