• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • Forums
  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings

Setting Up Python Virtual Environments on Windows

Tagged: Data science, Python, Virtual Environments, Windows

  • This topic has 0 replies, 1 voice, and was last updated 11 months, 1 week ago by IdowuIdowu.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • February 2, 2020 at 12:05 am #84965
    Idowu
    Participant
    @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

    image 1

     

    • Open up your file explorer, right click on My PC and click on properties
    • Click on Advanced Settings

    advanced settings image

     

    •  Click on Environmental Variable

    Environmental variables image

    •  Within the lower frame of your Environmental Variables, locate the name “Path”, if it doesn’t exist, click on New to create one:

    Lower frame image

    • Paste the file path as shown, ensure that you include a semi-colon after system32 before pasting the file path which you copied earlier:

    File path display image

    • 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

    Edit system variable

    • 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

    cmd1

    • You can also check Python version and enter Python shell within command line by just typing python

    cmd2

    • Exit the shell by typing exit() command

    cmd3

    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 python in the command line

    cmd5

    • To update pip, on the opened command line, type python  –m install  --upgrade pip command 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

    cmd4

    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 workon it 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 virtualenvwrapper and wait for it to install.

    cmd6

    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 mkvirtualenv for.

    cmd7

    • 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 deactivate in front of the environment:

    cmd8

    You’ll be back at the base prefix once you deactivate

    • To use the environment again, simply type the command workon for and you’ll be back in your Virtual Environment, this can be done anywhere on your PC:

    cmd9

    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:

    Folder image

    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:

    Envs folder image

    Using Virtualenv

    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 activate.

    Note that the command workon will not work in its case, remember, workon is 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 virtualenv to install virtualenv:

    cmd11 image

    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 mkdir command in command line, followed by the folder name, in my own case, I’ll be using my_envs:

    cmd13 image

    • 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:

    cmd14

    • Create a Virtual Environment in this directory by simply using virtualenv name command, e.g virtualenv tester, where name = tester:

    cmd15

    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:

    Check image

    Oh, it’s there.

    Now, let’s see what I was trying to Explain about workon in virtualenv

    • 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 workon is specific for environments created with the virtualenvwrapper:

    cmd16

    Now it’s giving you a recommendation to use virtualenvwrapper by creating it with mkvirtualenv

    • Let’s try to activate, without navigating into tester, still, this will return the following error:

    cmd18

    • How about we cd into the tester directory, then cd into the Scripts directory of tester and type activate as 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:

    cmd18

    Now we’re in the Virtual Environment

    • To deactivate, just use the deactivate command and you’ll be back in your base directory:

    cmd19

    Conclusion

    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.

     

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

Primary Sidebar

FORUM   MEMBERSHIP

Log In
Register Lost Password

POPULAR   FORUM   TOPICS

  • How to find the title of a song without knowing the lyrics
  • The Art of Exploratory Data Analysis (Part 1)
  • Welcome Message
  • How To Change Or Remove The WordPress Login Error Message
  • Getting Started with SQL: A Beginners Guide to Databases
  • Replacing The Default SQLite Database With PostgreSQL In Django
  • What is an ESN and how can it go bad?
  • Forums
  • About
  • Contact

© 2021   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy