简体   繁体   English

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

[英]PyCharm does not recognize modules installed in development mode

I have two pure python projects in PyCharm 3.4.1 Professional Edition. 我在PyCharm 3.4.1专业版中有两个纯python项目。 The first one, let's call it p (like package), is structured as a setuptools package (ie with setup.py, all requirements etc., however it is not uploaded to pypi or any other online repository). 第一个,我们称之为p (如包),结构为setuptools包(即使用setup.py,所有要求等,但它不会上传到pypi或任何其他在线存储库)。 The second one, let's call it s (like script), is just a python script along with two modules. 第二个,我们称之为s (如脚本),只是一个python脚本和两个模块。

Project s is (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) p in venv like this: 我遇到的问题如下:当我在venv安装项目(包) p ,如下所示:

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

in PyCharm in project s , import p statements are errorneous with message No module named p . 在项目s PyCharm中, import p语句与消息No module命名为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. 但是,当我在s运行脚本时,一切都很好,唯一的问题是PyCharm IDE抱怨无法找到模块。 I can live with this but it is very annoying... 我可以忍受这个,但它很烦人......

Why does this happen? 为什么会这样? Is it a PyCharm thing or packaging related thing? 它是PyCharm的东西还是包装相关的东西? See NEWS below. 见下面的新闻


The project/package p has 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 p to 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. PyCharm 5.0.4中仍然存在此问题。 However, I managed to solve it, kind-of. 但是,我成功地解决了它。

For some reasons I had to install another package from pypi. 出于某些原因,我不得不从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. 我通过PyCharm进入文件 - >设置 - >项目: - >项目解释器,点击绿色+ ,找到包并按下安装包按钮。 After the installation, the package installed by python3 setup.py develop is well recognized by PyCharm. 在安装之后, python3 setup.py develop安装的软件包得到了PyCharm的认可。 Obviously the problem was that PyCharm didn't have some cache in sync with reality. 显然问题是PyCharm没有一些缓存与现实同步。

So the new question is, can PyCharm be told to update its caches regarding the used python environment? 所以新的问题是,PyCharm可以被告知更新其有关使用过的python环境的缓存吗?

As of Pycharm 2016.1.4 (professional edition), it seems that opening a different project (eg 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 Recent)然后打开原始项目使Pycharm重新索引已安装的软件包,从而识别开发安装的软件包。

This is enough for me. 这对我来说已经足够了。

In the project pane, the directory which holds p needs to be marked as source. 在项目窗格中,保存p的目录需要标记为source。 Available under: Right-click -> "Mark Directory As" -> "Sources Root". 可用于:右键单击 - >“将目录标记为” - >“源根”。

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。

I had a devil of a time getting PyCharm to recognize a class in a module that I had just written. 我有一段时间让PyCharm认出我刚刚编写的模块中的一个类。

The problem is that PyCharm seems to default to not importing module class paths, which requires two separate fixes to correct. 问题是PyCharm似乎默认不导入模块类路径,这需要两个单独的修复来纠正。

Step 1 第1步

Right click on the module name, and mark it as "Source": 右键单击模块名称,并将其标记为“Source”:

在此输入图像描述

Step 2 第2步

For some reason, by default in PyCharm, it does not actually add directories marked as "Sources Root" to the Python path. 出于某种原因,默认情况下,在PyCharm中,它实际上并没有将标记为“Sources Root”的目录添加到Python路径中。 Fix this by switching this on. 通过打开它来修复此问题。

在此输入图像描述

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. Windows 10 x64。
  • PyCharm Community Edition 2017.2.3. PyCharm社区版2017.2.3。

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 添加你的包的路径(你开发的包)here.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 . 每个项目都引用一个解释器,您可以在settings-> project interpreter中找到它。 Now this interpreter uses a bunch of Paths to look for a library. 现在,这个解释器使用一堆Paths来查找库。 By default it has site-packages and bunch of other paths there. 默认情况下,它有站点包和一堆其他路径。
Now since you used pip develop -e or python setup.py develop , a dynamic link is created pointing to your package's repository instead of a package installation in site-packages directory. 既然你使用了pip develop -epython setup.py develop ,就会创建一个动态链接,指向你的包的存储库而不是site-packages目录中的包安装。 So our package's source path is not here, What we need to do is to add our source path to interpreter's Paths to make it work 所以我们的包的源路径不在这里,我们需要做的是将我们的源路径添加到解释器的路径以使其工作

Ref: 参考:

  1. https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000019690-PyCharm-not-recognize-development-library- 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 https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-reloading-interpreter-paths.html

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. 好像pycharm无法识别setup.py直接安装的模块,但可以识别由pip安装的模块。 Finally, I use pip install src_path , but I got to pip install it everytime I modify source code. 最后,我使用pip install src_path ,但每次修改源代码时都要进行pip安装

Sort of workaround that worked for me: 对我有用的解决方法:

Open both projects in PyCharm in the same window (workspace). 在PyCharm中的同一窗口(工作区)中打开两个项目。 Now open up the settings window, and under "Project -> Project Depencies" you can now select that project s depends on project p . 现在,打开设置窗口,并在“项目- >项目Depencies”你现在可以选择的项目s取决于项目p Imports and autocompletion will now work fine. 导入和自动完成现在可以正常工作。

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

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浴

Change the softlink of python to your /usr/bin/python path and things should work fine 将python的softlink更改为/ usr / bin / python路径,事情应该可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM