Loading video player…

Setting Up Your Environment: venv

Setting up and activating virtual environments with venv differs between Unix and Windows operating systems.

Creating and activating a virtual environment on a Unix system:

$ python3 -m venv ./venv
$ source venv/bin/activate

Creating and activating a virtual environment on a Windows system: :

Windows Command Prompt
c:\> c:\Python35\python -m venv c:\path\to\venv
c:\> venv\Scripts\activate.bat

Here are some resources and additional documentation about setting up virtual environments:

00:00 Another way is to use a virtual environment, which is essentially a standalone Python installation in a specified folder. Now, in order to do this, you need to use the venv module.

00:14 By typing into the console—whether you’re using a Windows, or a Mac machine, or a Linux machine—use your current Python program. In this case, I’d be using python3 and then using the virtual environment, or venv, module.

00:32 And then what this first command will do is—in your working directory, which you could go ahead and create a separate folder for this course—and then within that folder, run this first command and this will create a new folder called venv. And within this folder, what this first command will do is install a standalone Python installation. Now, this is a good thing to do in general so that you can install whatever modules you want, and all of those modules will be installed in this folder, and then you won’t have any conflicts with other versions of those same modules that maybe, say, you need for a different project or if you’re just experimenting with different versions.

01:15 So this first line, once you’ve done that, you then need to activate that Python installation, and so you would source that and then you navigate using this second line here to the activate script that’s contained inside the bin/ subdirectory of that venv/ folder that you just created.

01:34 And then you need to install the modules. So for this course, you’re going to need the numpy module, the matplotlib module, of course, pandas, and jupyter if you’re going to be using Jupyter for the course.

01:48 To install those modules, you can use pip3 install and then pass in the requirements.txt file. What this file will be is just, you can list the name of these modules, one on each line, and so that will allow you to install all of them at once.

02:07 And then once that’s downloaded and installed, you can then just open up a Jupyter Notebook by typing in jupyter notebook. That will fire up a Jupyter server. And I’ll show you how to do that, but once you’re all done and you’ve closed up your Jupyter tab and want to close down the browser, and you’ve done that as well, then you can deactivate the virtual environment that you activated up here, and that will take you back to the system Python that you had installed as your default Python version.

02:39 So let me show you how to do that real quick in the shell.

02:43 In your home directory or whatever directory that you’d like, go ahead and create a folder for this course. I created one in my home folder and I’ve called this folder the pandas_dataframe folder.

02:57 Let’s create the virtual environment, so go ahead and type python3, and then we’re going to be using the -m flag, which specifies the module, and the module is venv.

03:07 And we want to create, in this current working directory, a folder also called venv.

03:15 And when that’s done, if you take a look at the contents of the directory, we have the requirements.txt file, but then more importantly, we’ve created this virtual environment subfolder.

03:28 And let’s take a look at the requirements file.

03:32 So, this is a basic text file that contains the name of the modules that you want to install. Let’s clear that up. Now we need to activate the virtual environment, so that’s with source. In the current working directory, we’ve got the venv/ subdirectory that we just created, and then within that, we’ve got the bin/ directory, and then the activate script.

03:58 So run that, and then you’ll see that you’ve also got some indication that you’re working now with a Python installation in this virtual environment.

04:10 Now you want to use pip to install the modules contained in the requirements.txt file. This is the part that might take a bit because it’s going to require the modules to be downloaded and installed.

04:24 This might take a couple minutes depending on your computer and your internet connection.

04:30 This starts to download all the modules that are needed.

04:38 All right, and so once that is done, you could clear things up, and then if you type jupyter notebook

04:48 and run that, you’ll get a Jupyter server running and it will start in the folder where you executed that command, which, again, was our working directory that contained the virtual environment subfolder and then the requirements.txt file.

05:03 And then as before, you can open up a new Jupyter Notebook with Python 3, and then you’ll be ready to go just like before.

Become a Member to join the conversation.