使用现有的 virtualenv 配置 PyCharm

Configuring PyCharm with existing virtualenv

我正在尝试使用现有的 virtualenv 设置获取 PyCharm 运行。我已将我的 PyCharm 项目指向现有 virtualenv ~/.virtualenvs/myproj/ 中的 python 解释器

文件 -> 默认设置 -> 默认项目 -> Python 解释器

项目运行良好,但编辑器在作为 virtualenv 的一部分安装的包上仍然呈红色。

知道我错过了什么吗?

要 运行 PyCharm 正确地用于您的项目,您需要正确设置 Python 解释器和 Python 结构。

我已经正确设置了 Python 解释器,但错过了 Python 结构。

转到Pycharm->首选项->your_project->项目结构

添加正确的内容root

它与您的工作目录无关,您可以在 debug/run 配置中单独设置

也不要忘记添加您需要的环境变量,您应该可以开始了。

对于Intellij Idea 2016.2,以下是添加安装在virtualenv中的站点包的路径 文件 -> 项目结构 -> 来源 -> "Use + button and add as Sources"

如果隐藏目录可见,您可以更改视图设置或复制粘贴到 virtualenv 中站点包的路径

我做了 comiventor 在接受的答案中指定的操作,但还必须执行 Brian W 评论的操作:将根文件夹标记为 "Sources Root"。这是按如下方式完成的:

  • 右键单击您的根目录
  • 在底部查看选项 Mark Directory as
  • 选择Sources Root(文件夹图标颜色应从灰色变为蓝色)

就这些了!

不要单击 make available to all projects,它将起作用。否则会报错 -- specify a different sdk name

对于PyCharm 19.3

Ctrl + Alt + SFile / Settings

然后

Project: your-name-project / Project Interpreter / Gear Icon / Add ...

选择New environment

Location:

设置你的path / to / venv

最后,你应该看到

项目口译员:path / to / venv / Scripts / python.exe

Ubuntu 用户:

Step 1: Go to: "File > Settings > Python Interpreter"
Step 2: Drop down the Python Interpreter options and select "Show All..."
Step 3: Click on the left top "+" symbol to add your virtual environment
Step 4: Under Virtual Environment, select "Existing environment"
Step 5: By clicking the three dots at right side, link to the python 
        interpreter of your virtual environment. 
        For me, the linking looks like this: "/home/varat/cv/bin/python3"
        My virtual environment name is "cv"
Step 6: Finally, click "OK"

加载后,pycharm 将在您链接的虚拟环境下运行。