将 python 项目与虚拟环境相关联
Associating a python project with a virtual environment
一直在寻找这个但没有成功,我不知道我是否遗漏了什么但我已经有一个 virtualenv 但我如何创建一个项目来关联 virtualenv,谢谢
P.S. 我在 windows
我在这里可能是错的,但我不认为 virtualenv 本质上是你 与项目相关联 的东西。当你使用 virtualenv 时,你基本上是在说,"I'm taking this Python interpreter, installing what I want on it, and setting it aside from the Python interpreter that the entire computer uses by default." Virtualenv 没有 Python "project" 的概念;它只是一个自定义版本的 Python 解释器,您可以 运行 编写代码。像 PyCharm 这样的 IDE 中有一些工具可以让你将项目与 virtualenv 相关联,但这些是基础软件之上的另一层。
为了在项目中使用 virtualenv,您需要在每次使用时 "activate" 它。在 Windows 上激活 virtualenv 的文档可在 here.
找到
编辑:
看到您在 post 中标记了 virtualenvwrapper,所以我对此进行了一些搜索。似乎有 mkproject
命令,它创建一个项目文件夹,然后将其与 virtualenv 解释器相关联。可在 here.
中找到有关它的文档
要求:
- 虚拟环境
- Pycharm
转到虚拟环境并输入 which python
添加远程项目解释器(文件 > 默认设置 > 项目解释器 (cog) 添加远程)
您需要设置文件系统,以便 PyCharm 也可以打开该项目。
注意:
- 不要在未保存 运行 配置的情况下关闭虚拟环境,否则 会导致 pycharm 将您的 运行 配置视为损坏的
- 右上角有一个按钮,上面写着
share
启用此功能,您的 运行 配置将保存到 .idea 文件中,这样您的问题就会少很多
如果你已经安装了 virtualenv,你只需要开始使用它。
- 在
cmd
上使用 virtualenv env_name
创建您的项目虚拟环境。要将特定版本的 python 与您的环境相关联,请使用:virtualenv env_name -p pythonx.x
;
- 通过导航到其脚本文件夹并执行
activate
来激活您的环境。
- 您的终端现在正在使用您的虚拟环境,这意味着您安装的每个 python 软件包和您 运行 的 python 版本将是您在环境中配置的那些。
我喜欢创建名称与我的项目相似的环境,我总是为每个项目使用一个环境,这有助于跟踪我的特定项目需要哪些包运行。
如果您还没有阅读太多关于 venvs 的内容,请尝试使用谷歌搜索 requirements.txt
和 pip freeze
命令,这些对于跟踪项目的包非常有用。
我喜欢Pipenv: Python Dev Workflow for Humans管理环境:
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.
Pipenv is primarily meant to provide users and developers of applications with an easy method to setup a working environment.
一直在寻找这个但没有成功,我不知道我是否遗漏了什么但我已经有一个 virtualenv 但我如何创建一个项目来关联 virtualenv,谢谢
P.S. 我在 windows
我在这里可能是错的,但我不认为 virtualenv 本质上是你 与项目相关联 的东西。当你使用 virtualenv 时,你基本上是在说,"I'm taking this Python interpreter, installing what I want on it, and setting it aside from the Python interpreter that the entire computer uses by default." Virtualenv 没有 Python "project" 的概念;它只是一个自定义版本的 Python 解释器,您可以 运行 编写代码。像 PyCharm 这样的 IDE 中有一些工具可以让你将项目与 virtualenv 相关联,但这些是基础软件之上的另一层。
为了在项目中使用 virtualenv,您需要在每次使用时 "activate" 它。在 Windows 上激活 virtualenv 的文档可在 here.
找到编辑:
看到您在 post 中标记了 virtualenvwrapper,所以我对此进行了一些搜索。似乎有 mkproject
命令,它创建一个项目文件夹,然后将其与 virtualenv 解释器相关联。可在 here.
要求:
- 虚拟环境
- Pycharm
转到虚拟环境并输入 which python
添加远程项目解释器(文件 > 默认设置 > 项目解释器 (cog) 添加远程)
您需要设置文件系统,以便 PyCharm 也可以打开该项目。
注意:
- 不要在未保存 运行 配置的情况下关闭虚拟环境,否则 会导致 pycharm 将您的 运行 配置视为损坏的
- 右上角有一个按钮,上面写着
share
启用此功能,您的 运行 配置将保存到 .idea 文件中,这样您的问题就会少很多
如果你已经安装了 virtualenv,你只需要开始使用它。
- 在
cmd
上使用virtualenv env_name
创建您的项目虚拟环境。要将特定版本的 python 与您的环境相关联,请使用:virtualenv env_name -p pythonx.x
; - 通过导航到其脚本文件夹并执行
activate
来激活您的环境。 - 您的终端现在正在使用您的虚拟环境,这意味着您安装的每个 python 软件包和您 运行 的 python 版本将是您在环境中配置的那些。
我喜欢创建名称与我的项目相似的环境,我总是为每个项目使用一个环境,这有助于跟踪我的特定项目需要哪些包运行。
如果您还没有阅读太多关于 venvs 的内容,请尝试使用谷歌搜索 requirements.txt
和 pip freeze
命令,这些对于跟踪项目的包非常有用。
我喜欢Pipenv: Python Dev Workflow for Humans管理环境:
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.
Pipenv is primarily meant to provide users and developers of applications with an easy method to setup a working environment.