- This topic has 0 replies, 1 voice, and was last updated 8 months ago by Idowu.
- February 2, 2020 at 12:05 am #84965Participant@idowu
Setting up a virtual environment while building a Data Science project or application is important. As necessary as this is, it’s mostly avoided either deliberately or ignorantly by Data Scientists and this is especially common with newbies. Even some experienced Python programmers are also not left behind.
If you don’t use a Virtual Environment, one question remains; how do you manage dependencies and build a full grown project that’s ready to be consumed by the world? Obviously, without a good environmental set-up, it becomes almost impossible not to get things messed up along the line.
There are several ways in which you can set up a Virtual Environment in Python and in this article, I’ll walk you through some of the coolest methods you should always use.
What is a Virtual Environment?
You might think of a Virtual Environment as an isolated, unreal kind of working space on your machine, which allows you to work independently off the machine-based python version. In other words, it enables you to easily manage dependencies.
By dependencies, it means you’ll be able to use different versions of Python and its libraries as well as web frame works where applicable to manage different projects at a time without package interaction.
Take for instance, you have about three different projects on your local machine and your intention is to use Python 2.7 for one, then Python 3.7 for another, you also want to use different versions of Flask. A Virtual Environment will allow you to be able to maintain version control over each of the three projects.
Now let’s take a deeper dive together into setting up your Environment on Windows OS
Firstly, Add Python to your System Base Path
To do this, you only need to follow the following few steps:
- Go to the directory where you installed Python on your PC and Copy its file path
- Open up your file explorer, right click on My PC and click on properties
- Click on Advanced Settings
- Click on Environmental Variable
- Within the lower frame of your Environmental Variables, locate the name “Path”, if it doesn’t exist, click on New to create one:
- Paste the file path as shown, ensure that you include a semi-colon after system32 before pasting the file path which you copied earlier:
- You might also want to add the Scripts path of Python to path, to do this, just add a semi-colon in front of the path you added previously, paste the path in front of the semi-colon, add the Script path by just putting \Scripts in front of your file path
- Click on OK
- You’re now done adding python to your system path
- To verify if this was successful, just open command prompt anywhere on your PC and type the command
where python, press Enter; this will return the full path
- You can also check Python version and enter Python shell within command line by just typing
- Exit the shell by typing
Note that it was necessary to add python to your base path because you’re sure going to be using python in command line, failure to add it to path returns an exception that tells you that
Python is not recognized as an operable command
Get your Virtual Environment Ready by using pip
Pip is a package which allows you to rapidly install packages automatically without you having to visit the web page to download and install them.
However, it’s most likely that the Python version you installed comes with
pip, but it could be out of date, so you’ll need to update pip version. To do this:
- Once again, open the command line by typing “cmd” at the location where you have Python installed or you can just type
where pythonin the command line
- To update pip, on the opened command line, type
python –m install --upgrade pipcommand or you can just install pip afresh if you so wish by running python
get -pip.py(this should be done by downloading pip.py file, navigating to its folder on your PC and running the command
get -pip.py), these two commands, whichever one you choose to execute, will have pip updated or installed afresh respectively
Please, also take note that you’ll need internet connection for all these
Great! Now that pip is installed or updated, next, we should install virtualenvwrapper. Please note that there are other ways of creating Virtual Environments. Other than the virtualenvwrapper, conda or virtualenv can also be used, but I recommend virtualenvwrapper or virtualenv. Depending on whichever one you choose, We will have a look at both, although, personally, I prefer the virtualenvwrapper.
An advantage of using virtualenvwrapper is that it wraps all the environments in one single file, usually in a file called Envs, thereby preventing the environments from being located just anywhere on your machine. A virtualenv on the other hand requires that you maybe create a folder, navigate to that folder and create your virtualenv in it.
However, in contrast, virtualenvwrapper is easy to use, as you can actually
workonit anywhere on your machine without having to navigate to the exact location of your Virtual Environment’s Scripts.
Using the Virtualenvwrapper
- To install virtualenvwrapper, simply type the command
pip install virtualenvwrapperand wait for it to install.
Now you’ve successfully installed your virtualenvwrapper. Supercool!
Let’s Make a Virtual Environment with the virtualenvwrapper
- This is done by opening cmd anywhere on your PC, use the command
mkvirtualenv name_of_env, e.g, if my environment’s name is “for”, I will type
- Now you’re in your Virtual Environment, with for in bracket, you can start pip installing packages, e.g you can
pip install python==3.5,
pip install pandas,
pip install flask, etc. This will allow you to work on your projects with specific packages and python 3.5 alone in that particular environment.
- You can deactivate your virtual environment by typing the command
deactivatein front of the environment:
You’ll be back at the base prefix once you deactivate
- To use the environment again, simply type the command
workon forand you’ll be back in your Virtual Environment, this can be done anywhere on your PC:
You can see that I created a Virtual Environment on the base directory of my PC using virtualenvwrapper, this doesn’t mean that my Virtual Environment for in this case is located there exactly, however, it is wrapped up automatically in an auto-created folder with other environments in a folder called Envs, let me show you what I’m trying to explain:
You can see the folder Envs, I didn’t create that, it was made by the virtualenvwrapper and when you open the folder Envs, it will contain all the environments I’ve created:
As for virtualenv, you might just want to create a folder manually where you want your environments to be, this is because, to activate such environments, you can only use the command
Note that the command
workonwill not work in its case, remember,
workonis specific for virtualenvwrapper. The same is not true for virtualenv, for virtualenv, you must be within the Scripts directory of your Virtual Environment before you can activate it, that can be a little bit confusing and stressful, but let’s still take a look:
- Open up command prompt and use the command
pip install virtualenvto install virtualenv:
The message above was shown because I already have virtualenv installed on my machine
- To make a Virtual Environment with virtualenv, simply create a folder where you want it to be, by typing
mkdircommand in command line, followed by the folder name, in my own case, I’ll be using my_envs:
- Cd into the directory by typing
cd folder_name, e.g
cd my_envs, now you are in the folder where you want all your environments to be:
- Create a Virtual Environment in this directory by simply using
virtualenv namecommand, e.g
virtualenv tester, where name = tester:
Now you’ve made a Virtual Environment using virtualenv
- To confirm, let’s view the location of my_envs to see if tester is there:
Oh, it’s there.
Now, let’s see what I was trying to Explain about
- In the command line, type
workon tester, this should not work because you’ve used the virtualenv and not virtualenvwrapper to make the Virtual Environment this time around. It will return an exception, telling you tester does not exist, this is because the command
workonis specific for environments created with the virtualenvwrapper:
Now it’s giving you a recommendation to use virtualenvwrapper by creating it with
- Let’s try to
activate, without navigating into tester, still, this will return the following error:
- How about we
cdinto the tester directory, then
cdinto the Scripts directory of tester and type
activateas shown? This will work. Therefore, you must be within the Scripts directory of your environment while using virtualenv before you can activate it, else, it won’t work:
Now we’re in the Virtual Environment
- To deactivate, just use the
deactivatecommand and you’ll be back in your base directory:
Working in a virtual environment gives you full control over your project. However, whether you use virtualenvwrapper, virtualenv or conda –create, they all achieve the same thing; which is creating a Virtual Environment. Although, the virtualenvwrapper makes it easier to navigate between environments. Whichever one is easier for you to use, you’re free to choose, just ensure that you work in a virtual environment, but I’m quite sure many will choose the virtualenvwrapper as a result of its user friendliness.
- You must be logged in to reply to this topic.