简体   繁体   中英

Trying to create a virtualenv in python and activate it

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

In command prompt,

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.

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.

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

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

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

IE if when running it said :

C:\Users\UserName>virualenv ENV

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

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


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 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.


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

ENV\Scripts\deactivate 

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

When you create your virtual environment you use:

virtualenv .\ENV

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

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

 .\ENV\scripts\activate

(once again notice the .\\ )

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

The virtualenv instructions are here for complete reference

Can you explain why would you want to activate it? From Python Docs :

You don't specifically need to activate an environment; activation just prepends the virtual environment's binary directory to your path, so that “python” invokes the virtual environment's Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment's Python automatically.

On the same link you can see the commands you need with more information.

PS Take a look at another packaging tool, Pipenv . It is easy and will save your time. I highly recommend it.

virtual environment is used to create the room where you can use the specific package for specific project.

to Activate the virtualenv in linux.

once you installed the virtualenv in system

use the command,

ritik@ritik-MS-7A15:~$ virtualenv jog

these command creates An virtualenv

2.To Activate the virtualenv

ritik@ritik-MS-7A15:~$ source hog/bin/activate

these command will Activate the virtual environment.And you'll get this

(hog) ritik@ritik-MS-7A15:~$

3.To deactivate the virtualenv use

(hog) ritik@ritik-MS-7A15:~$ deactivate

it will deactivate the virtual environment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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