This article focuses on setting up a virtual environment for Python development on Windows. Steps To Set Up Virtual Environment For Python On Windows
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 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:\Python\Python36" 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 the "C:\Python\Python37" 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,
You can now copy the value in your favorite text editor and look at it. Mine looks like the following.
C:\Python\Python37\Scripts\;C:\Python\Python37\;C:\Python\Python36\Scripts\;C:\Python\Python36\; //and a 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.
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,
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:\Python\Python36\python.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:\Python\Python36". 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 .\venv\Scripts\activate // 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.
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.
In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc.. You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like __init__, __call__, __str__ etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
The OS module is a python module that provides the interface for interacting with the underlying operating system that Python is running.