尝试在 python 中创建一个 virtualenv 并激活它 [英] Trying to create a virtualenv in python and activate it

查看:23
本文介绍了尝试在 python 中创建一个 virtualenv 并激活它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在 Python 中创建一个 virtualenv 并激活它.我正在使用 Windows 操作系统.

I am having a hard time to create a virtualenv in Python and activating it. I am working on the Windows Operating system.

在命令提示符中,

我已经通过输入以下命令安装了 virtualenv:

I have installed virtualenv by typing the following command:

pip install virtualenv

然后,为了在其中创建虚拟环境,我使用了以下内容:

Then, to create a virtual environment in it, I used the following:

virtualenv ENV

但是,我不确定这是否正确.

But, I am not sure if this right.

接下来,我无法理解如何激活它.对于 Linux,我发现可以使用 source/bin/activate 完成激活.但是,无法找到适用于 Windows 的那个.

Next, I am unable to understand how to activate it. For Linux, i found that activation would be done using source /bin/activate. But, unable to find the one for Windows.

请帮我创建 virtualenv 并激活它.

Please help me out in creating the virtualenv and activating it.

推荐答案

您已正确安装.您给出的命令:virtualenv ENV 将创建一个名为 ENV 的文件夹并将安装放在其中.

You've installed it correctly. The command you gave: virtualenv ENV will create a folder called ENV and place the installation inside it.

字典将在 shell 中指定的路径中创建.

The dictionary will be created in the path specified in the shell.

IE 如果在运行时说:

IE if when running it said :

C:\Users\UserName>virualenv ENV

ENV 文件夹将被放置在 C:\Users\UserName 中.

the ENV folder will be placed int C:\Users\UserName.

这绝对没问题.请注意,您不必一直将其称为 ENV.

This is absolutely fine. Note that you don't have to call it ENV all the time though.

要激活,您需要导航(在 shell 中使用命令 cd)到安装 virtualenv 的位置.一旦你进入

To activate you would need to navigate (in the shell using the command cd) to the location where virtualenv is installed. Once there you enter

ENV\Scripts\activate 

activate 是一个批处理脚本,它将更改您的终端为具有 (ENV)(或您在 shell 路径开头选择的任何文件名.当您看到它时,它告诉您它已被激活.

activate is a batch script that will change your terminal to have (ENV) (or whatever filename you choose at the beginning of the shell path. When you see this it tells you it has been activated.

要停止虚拟环境,您需要使用 deactivate.这可以以相同的方式使用.IE 是这样的:

To stop the virtual environment you need to use deactivate. This can be used in the same way. IE like this:

ENV\Scripts\deactivate 

<小时>

以防万一您使用 PowerShell 而不是命令提示符:

在 PowerShell 上有执行策略.这意味着还有其他适用的操作:


Just in case your using PowerShell and not Command Prompt:

On PowerShell there are execution policies. This means there are additional actions that apply:

在开始之前,系统上的所有脚本都必须经过数字签名才能执行.你可以这样做:

Before starting allow all scripts on the system must be digitally signed to execute. You can do it like this:

Set-ExecutionPolicy AllSigned

创建虚拟环境时使用:

virtualenv .\ENV

(注意 .\ 而不仅仅是文件夹名称)

(notice the .\ instead of just the folder name)

接下来运行使用类似(但不同)的命令:

Next to run use the similar (but different) command:

 .\ENV\scripts\activate

(再次注意.\)

当提示您需要接受执行时,只需输入 Y.已激活.

When prompted you will need to accept the execution just enter Y. It has been activated.

virtualenv 说明在这里供完整参考

这篇关于尝试在 python 中创建一个 virtualenv 并激活它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆