Python PyCharm 在 virtualenv 中找不到包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34520291/
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
PyCharm cannot find the packages in virtualenv
提问by ruipacheco
I have the latest PyCharm CE and am using it with virtualenv
. I have defined the interpreter as the interpreter in the virtualenv
. The Project Interpreter window in PyCharm lists all the packages I have installed. I confirmed this by running pip freeze > requirements.txt
and running through the packages manually.
我有最新的 PyCharm CE 并且正在将它与virtualenv
. 我已将解释器定义为virtualenv
. PyCharm 中的 Project Interpreter 窗口列出了我安装的所有包。我pip freeze > requirements.txt
通过手动运行和运行包来确认这一点。
My problem is that PyCharm won't find certain includes in its editor windows, like Flask-Login
:
我的问题是 PyCharm 不会在其编辑器窗口中找到某些包含,例如Flask-Login
:
In from flask.ext.login import current_user, login_user, logout_user, login_required
the includes current_user, login_user, logout_user, login_required
are all marked as unresolved references.
在from flask.ext.login import current_user, login_user, logout_user, login_required
包含current_user, login_user, logout_user, login_required
中都标记为未解析的引用。
Am I missing something?
我错过了什么吗?
采纳答案by Nathaniel Ford
The problem may lay in PyCharm picking up faulty 'Interpreter Paths' for your virtual environment. Go here:
问题可能在于 PyCharm 为您的虚拟环境挑选了错误的“解释器路径”。到这里:
PyCharm (menu) -> Preferences (Menu option)
-> Project: <name> (Dropdown)
-> Project Interpreter (Menu option)
-> 'Settings' button (Looks like a gear)
-> More (Menu option)
-> Select your virtualenv interpreter
-> Click 'Show paths for interpreter' button (on bottom of list window)
Now that you're in this (admittedly tortuously found) location, you should see paths being used by this interpreter. If my theory is correct, these are pointing to global system locations. To add the virtual environment paths, you should click the +
button and add corresponding paths that exist inside your virtual environment. Once you're done with this, it's a good idea to select the global system paths and click -
to remove them. Click apply, and go to File -> Invalidate caches / Restart
to reload PyCharm.
既然您在这个(公认是曲折地找到的)位置,您应该会看到此解释器正在使用的路径。如果我的理论是正确的,那么这些都指向全局系统位置。要添加虚拟环境路径,您应该单击+
按钮并添加虚拟环境中存在的相应路径。完成此操作后,最好选择全局系统路径并单击-
以将其删除。单击应用,然后转到File -> Invalidate caches / Restart
重新加载 PyCharm。
This should get your interpreter to be pointed to the correct location for the libraries you've installed into your virtualenv, and you should no longer be getting the import error. Note that even with this fix you will not see your libraries under the Project Interpreter
, but they should be being loaded.
这应该让您的解释器指向您安装到 virtualenv 中的库的正确位置,并且您应该不再收到导入错误。请注意,即使使用此修复程序,您也不会在 下看到您的库Project Interpreter
,但它们应该正在加载。
回答by J.Ku
Open up Preferences -> Project -> Project Interpreter
, do you see the module there?
打开Preferences -> Project -> Project Interpreter
,你看到那里的模块了吗?
If yes, you might have another file somewhere in your project have the same name as flask.ext.login
, this prevents pycharm from locating the actual module.
如果是,您的项目中可能有另一个与 同名的文件flask.ext.login
,这会阻止 pycharm 定位实际模块。
If no, you can click on the ...
beside your interpreter and select more...
, select your interpreter and at the bottom (beside the filter), click the Show paths for the selected interpreter
, you can add the path of your module there.
如果没有,您可以单击...
解释器旁边的 并选择more...
,选择您的解释器,然后在底部(过滤器旁边)单击Show paths for the selected interpreter
,您可以在那里添加模块的路径。
回答by dieend
In the newest version of PyCharm (2016.1.4 in my case):
在最新版本的 PyCharm 中(在我的例子中是 2016.1.4):
- Settings
- Project: name of project
- project Interpreter
- at the right side there will be a dropdown where you can choose the interpreter. There should be venv options.
- 设置
- 项目:项目名称
- 项目翻译
- 在右侧会有一个下拉菜单,您可以在其中选择口译员。应该有 venv 选项。
See image below for better explanation (like they said, 1 picture worth thousands word)
请参阅下图以获得更好的解释(就像他们说的,一张图片价值数千字)
回答by Eyedia Tech
回答by Eric Lou
Goto /venv/bin/ and check all activate scripts. You venv path might be wrong.
转到 /venv/bin/ 并检查所有激活脚本。您的 venv 路径可能是错误的。
回答by jagadish
I was not able to assign existing virtual environment to my project, but after going to
我无法将现有的虚拟环境分配给我的项目,但是在转到
File -> Settings -> project interpreter-> show all-> click on '+'
文件 -> 设置 -> 项目解释器 -> 全部显示 -> 点击“+”
to create a new virtual environment or we can choose the existing virtual environment, I am able to assign and use the existing virtual enviroments.
创建一个新的虚拟环境或者我们可以选择现有的虚拟环境,我可以分配和使用现有的虚拟环境。
回答by Ben Yitzhaki
For me, the easiest solution was to open the project in the root directory (my project has a server and client directories, thus the root directory contained both of them). When you open the project in the root directory, it is able to find the dependencies without messing with pycharm settings as it uses them by convention.
对我来说,最简单的解决方案是在根目录中打开项目(我的项目有服务器和客户端目录,因此根目录包含它们)。当您在根目录中打开项目时,它能够找到依赖项而不会弄乱 pycharm 设置,因为它按照惯例使用它们。
回答by Joseph Pleuss
I noticed that every time I open a different project it still has the venv from the project I was previously working on.
我注意到每次我打开一个不同的项目时,它仍然有我之前工作的项目的 venv。
What I do is:
我要做的是:
ctrl-alt-s (to go into preferences), then Project Interpreter/settings (gear icon), show all, then remove all the venv environments that aren't your current project (use the - sign). Restart, and you should be good to go.
ctrl-alt-s(进入首选项),然后是项目解释器/设置(齿轮图标),全部显示,然后删除所有不是您当前项目的 venv 环境(使用 - 符号)。重新启动,你应该可以开始了。
回答by ggrelet
My two cents on this topic as I struggled myself with it recently. Nathaniel Ford's answer is the good one except that this part:
我最近在这个话题上挣扎了两分钱。纳撒尼尔福特的回答是好的,除了这一部分:
-> Select your virtualenv interpreter
was unclear to me.
我不清楚。
I tried several times with
我试过几次
~/.virtualenvs/python-audition-2.9/bin/python
whereas it only worked with
而它只适用于
~/.virtualenvs/python-audition-2.9/local/bin/python
Notice the .../local/...
in the latter path? It was really important in my case. And don't forget to File -> Invalidate caches / Restart
to reload PyCharm.
注意到.../local/...
后面的路径了吗?这对我来说非常重要。并且不要忘记File -> Invalidate caches / Restart
重新加载 PyCharm。