简体   繁体   English

Pycharm未解析virtualenv中的引用

[英]Pycharm unresolved reference in virtualenv

When I started a new project in Pycharm using virtual environment, I got an unresolved reference warning message. 当我使用虚拟环境在Pycharm中启动一个新项目时,我得到了一个未解决的参考警告消息。

在此输入图像描述

But I have installed django to my virtual environment and this code is working correctly. 但是我已经在我的虚拟环境中安装了django,这段代码运行正常。

How can I fix this? 我怎样才能解决这个问题? I'm using PyCharm 4.5.2 Pro 我正在使用PyCharm 4.5.2 Pro

Open your project in pycharm and follow the step: 在pycharm中打开您的项目并按照以下步骤操作:

click on file(on top) > settings > project interpreter > add local > select virtualenv 单击文件(在顶部)>设置>项目解释器>添加本地>选择virtualenv

                      OR

Make sure your project is a root directory, make it is the root directory. 确保您的项目是根目录,使其成为根目录。

I eventually stumbled across a method that worked for me, but I don't know the underlying problem. 我最终偶然发现了一种对我有用的方法,但我不知道潜在的问题。

Initially I created a virtualenv with PyCharm in the interpreter options. 最初我在解释器选项中使用PyCharm创建了一个virtualenv。 Annoyed that I was having this problem, I deleted it and just created a virtualenv with the virtualenv command, but the problem remained. 我很生气,我遇到了这个问题,我删除了它,只是用virtualenv命令创建了一个virtualenv,但问题仍然存在。

Then I went back to the interpreter selector and picked "add local". 然后我回到解释器选择器并选择“添加本地”。 I navigated to the python binary in the /bin folder of the virtualenv I created and selected it. 我导航到我创建的virtualenv的/ bin文件夹中的python二进制文件并选中它。 After this, the unresolved references were fixed. 在此之后,未解决的引用被修复。

I think your problem is like mine. 我认为你的问题就像我的问题。 I tested import matplotlib in the python shell, and it works, but in Pycharm it shows an error. 我在python shell中测试了import matplotlib ,它可以工作,但在Pycharm中它显示错误。 This is because Pycharm doesn't know the path of site-packages. 这是因为Pycharm不知道站点包的路径。

To fix this, you can do one of the following: 要解决此问题,您可以执行以下操作之一:

  • Add the path of site-packages (for me C:\\Program Files\\Python 3.5\\lib\\site-packages) to PATH. 将站点包的路径(对于我C:\\ Program Files \\ Python 3.5 \\ lib \\ site-packages)添加到PATH。

  • Go to File -> Settings -> Project Interpreter, then choose Python real path instead of venu path . 转到文件 - >设置 - >项目解释器,然后选择Python真实路径而不是venu路径 在此输入图像描述

  • In your code add the following: 在您的代码中添加以下内容:

.

import sys    
sys.path.append(r"C:\Program Files\Python 3.5\lib\site-packages")
import matplotlib.pyplot as plt

I think it will help you :) 我认为它会帮助你:)

Check your project root directory. 检查项目根目录。 Maybe problem in it. 也许是问题所在。

And did you create virtualenv manually or through PyCharm? 你是手动还是通过PyCharm创建virtualenv?

文件>设置>项目>项目结构

File > Settings > Project > Project Structure Double Click on the folder link under Content Root, and do OK. 文件>设置>项目>项目结构双击“内容根”下的文件夹链接,然后单击“确定”。

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

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