Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 499
prudhvi Avatar asked Jan 28 '23 20:01

prudhvi


2 Answers

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

like image 135
Xantium Avatar answered Jan 31 '23 20:01

Xantium


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.

like image 34
whiplassh Avatar answered Jan 31 '23 20:01

whiplassh