How to Install Python
To begin, we will need to make sure that Python is installed on our system so that we may begin creating projects. There are a multitude of ways to install Python, but let’s go for the simplest way.
The simplest way to install Python would be visiting the python.org website and heading to its downloads page, if you want direct access to the downloads page, use the following link : python.org/downloads. Once there, it should look something like the following :
We can click the yellow button which will download the latest stable version of Python, then a download will start, once completed open the file now in your downloads folder.
You will be met with this installer, very important that the tickbox for “Add python.exe to PATH” is ticked off. Then we can simply click the Install now button, let the installer run and the installation should be successful and you will be met with the following :
Finally, to confirm that Python is actually installed on our system, we can search for the command prompt :
Then once opening it, entering the following command : python –version.
If Python is successfully installed, it will display the current version that we just installed. Now we are good to go and you may start Setting up a Virtual Environment for Python.
Setting up a virtual environment for Python
Now that we are set up with VSCode and Python, let’s make our project management even better by making use of virtual environments, this helps us keep our Python installation clean by isolating our different projects, this also assists with reducing clashing versions between our projects and make it so that it is easy to reproduce project environments.
Let's get started by setting up a basic virtual environment.
Firstly, we can create our environment within VSCode, firstly while we have our project open, we can right click in our project window and select “Open in Integrated Terminal” :
Once we have selected the option, we should see a cmd / terminal window open up with it already navigated to our project folder. We are now ready to start creating our virtual environment.
Let’s take a look at how we can quickly have a virtual environment created for our use.
In our integrated terminal, we are going to enter the following commands, in order :
python / python3 -m venv myenv
cd myenv/Scripts
activate
What the above commands do is essentially the basis of creating our environment, python -m venv myenv is the command to create the virtual environment, the name myenv is what we named our virtual environment, we can name it whatever we please, but this should suffice for our needs.
Then we path into the new file called myenv and then into a subfolder called Scripts. At this point we should be ready to activate our virtual environment, we then finally enter activate to activate the env. To confirm that all of this worked, you should see in your terminal, encaused in brackets the name of your virtual environment.
With that now complete, we are free to install all needed packages for our projects
Remember that the usefulness of virtual environments stems from the ability to create separate virtual environments for each project to avoid conflicts.
Let us now take a look at how the virtualenv version would work :
Instead of going straight into creating the virtual environment, we first must install the virtualenv module, the use of the module would be to create a virtual environment should the other method show any issues or inconsistencies.
After installing the module, it would be the exact same process with slight differences :
Should you wish to disable or exit the virtual environment that you are using, simply enter the following command :
By entering deactivate, we disable the virtual environment and we can work on another project without using the one we were recently on.
Then we can activate the environment once more to continue working on your project. Happy Coding!