Steps To Set Up Virtual Environment For Python On Windows

综合编程 2018-08-22 阅读原文

Introduction

Earlier, I wrote an article on
how to set up a Virtual Environment for Python development on Ubuntu . This article, as the title suggests, focuses on setting up a virtual environment on Windows. I’m using Windows 8, and everything we will do here should work on other versions of Windows as well.

Installing and Running Python

Installing Python, or any other software, for that matter, is fairly easy on Windows. You can download the Python installer from the Download Python page of the official website. The versions I will be using for this article, are 3.6 and 3.7. Please feel free to pick your own versions.

While installing Python 3.6, please make sure that you have selected the checkbox for "Add Python 3.6 to PATH". Also, note that I'm installing Python in "C:PythonPython36" directory, which would be useful later. So, I would recommend you to do the same, however, you are not bound to.

Once done, open up your PowerShell, type "python" and hit enter/return. You should see an output similar to mine. You can exit Python by using the quit() or exit() call.

It's time to install Python 3.7 in "C:PythonPython37" directory. Please ensure that you select the checkbox for "Add Python 3.7 to PATH". Once done, close the current PowerShell, and open it again. Then type "python --version" and hit enter/return. You should see an output similar to this one:

Notice how "python --version" returns the version to be "Python 3.7.0" and not "Python 3.6.6". The reason for this is the value of the PATH environment variable. To see the value you need to,

  • right click Computer and select Properties
  • select Advance System Settings
  • from the pop-up box select Advanced tab
  • select Environment Variables
  • select PATH and then Edit

You can now copy the value in your favorite text editor and look at it. Mine looks like the following.

C:PythonPython37Scripts;C:PythonPython37;C:PythonPython36Scripts;C:PythonPython36;    
//and few other values

When we execute the command "python" or "python --version", it looks into the "PATH" variable and finds Python 3.7 as the first match, it stops there and looks no further. Therefore, if we want to run "Python 3.6", we have to manually change the "PATH" variable or change the installation order which will, in turn, change the variable.

However, there is a way to run a specified version of Python, and we can do so by using the command "py -". Please refer to the image below.

Setting Virtual Environment

To set up a virtual environment, we first need to install the package virtualenv using pip. To do so, open up your PowerShell and execute the following commands.

// upgrade pip to its latest version  
python -m pip install --upgrade pip   
  
// install virtualenv  
pip install virtualenv  

If your requirement falls under any of the following categories,

  • have only one Python version installed
  • don't want to specify any Python version
  • want to use default Python version (check your version by running "python --version" on the command line)

Then, you can simply create your virtual environment using the "virtualenv venv" command, where "venv" is the environment name. However, if none of the above categories satisfies your requirement, then follow along as it's time to create your virtual environment using with Python 3.6.

// navigate to Desktop  
cd .Desktop  
  
// create a new directory 'project-36'  
mkdir project-36  
  
// change currenct directory to 'project-36'  
cd .project-36  
  
// create a virtual environment named 'venv', feel free to name it anything you like
virtualenv venv -p C:PythonPython36python.exe  

Notice the last command. With the attribute "-p" we have specified the Python version that we want our virtual environment to use. In our case, it's Python 3.6, which we had installed at "C:PythonPython36". If you installed it at a different location, please pass the complete path here.

If you want to use Python 3.7 instead, all you need is to change the installation path for Python in the last command. Just put in the path where you installed Python 3.7 and you are good.

Now, it's time to activate the environment, check the Python version and also list the default packages installed for us. To do so, execute the following commands and you should see a similar output as shown in the image that follows. The (venv) on the left shows that our virtual environment is active.

// activate the virtual environment 
.venvScriptsactivate

// check the python version
python --version

// list all packages installed by default
pip list

// deactivate the virtual environment
deactivate

Congratulations! You have successfully created your first virtual environment for Python. And, you are now all set to start your journey with Python development over Windows.

Summary

In this article, we successfully -

  • installed Python (different versions)
  • learned how to run specific Python version over command line
  • created virtual environments, running different versions of Python, using virtualenv package

If you are interested to learn more and set up your Python virtual environment on Ubuntu (or other Linux based system), do check out myprevious article where I walk you through each step in detail.

C-Sharpcorner

责编内容by:C-Sharpcorner阅读原文】。感谢您的支持!

您可能感兴趣的

政治不正确?Redis/Python等的master-slave要改名了。... Master/Slave模式,是分布式系统中一个比较常用的计算结构。这个词最开始来源于MySql数据库,主要表示MySQL数据库自身提供的主从复制功能,通过master-slave,可以方便的实现数据的多处自动备份,实现数据库的拓展。多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提...
Get the window handle of a program running through... There is an externally running program that i need the capability to resize. The kicker for me is that part of the title is the version and other spec...
Virtual Environments Demystified Here’s a non-exhaustive list of programs that are all meant to help create or manage virtual environments in some way: Hatch , VirtualEnvMan...
1.1 Python for macOS 安装与配置 本文主要讲解在macOS系统下的Python3.7.0的配置与安装问题 并调试好开发环境 目标是编辑成功第一个python程序 下载最新版(3.7.0)Python macOS系统自带python 不过是较低的版本(2.7.10) 可以在终端中输入"python"来...
Iron Python vs. Razor I have a little bit of experience with the new Razor syntax, but none with Iron Python. I was wondering do both meet the same needs? Is one favored...