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
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.
P.S. Take a look at another packaging tool, Pipenv. It is easy and will save your time. I highly recommend it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With