09 Beginner’s Guide to Python – Virtual Environments

In this video I’m going to explain how
you use Python virtual environments. One of the most powerful features of Python
is it allows you to install other people’s Python applications as modules
in your project. So if somebody has already created and published a package
to do something you don’t need to reinvent the wheel you can just install
that package and then you can add whatever logic on top of that that you
need for your custom application. In the next video I’m going to explain how you
install and use Python packages. As a prerequisite to that I’m going to
explain how to create a virtual environment. Virtual env is a tool that
is provided with Python by default. It’s a tool that allows you to group Python
packages to a specific project. So normally when you would install a
package it would install into the base Python installation of that operating
system. In order to avoid cluttering the base operating system you can group all
the Python dependencies for your particular project in a virtual
environment. That way you can keep a separate set of dependencies for your
project separate from other projects or the actual Python packages that are used
in your operating system if your operating system uses Python for any
applications that you have installed. So let’s go ahead and create our first
Python virtual environment. Alright so make sure we have the Atom
editor open and the command prompt or the terminal window if you’re using Mac
or Linux. We’re not actually going to be creating any Python code in this video
but we’re going to have this open just so we can see the changes that are made
to our files when we create the Python virtual environment. Okay so let’s go
ahead and create our first virtual environment. The way you create a virtual
environment using Python 3 is you type Python – M V env that stands for virtual
environment and then the name of the virtual environment you want to create.
Common convention is to create it in the project with the name env so you know
that this is the virtual environment for this project. If we hit enter it will go
ahead and create a virtual environment. You can see that it’s created this
folder here and it’s going to begin to populate it with all the basic
dependencies for a Python virtual environment. If you’re using Windows this can take a minute or two so feel free to pause the video
and then we’ll continue once that is done. Okay so now our Python virtual environment has been created. So it’s basically
created this folder here and inside it is our Python virtual environment. Now we shouldn’t actually touch any of the files that are inside here, so you never
want to modify any of these files directly
they’re just there to run in the background for our project. The way that
virtual environments work is whenever you install a Python dependency it gets
installed into the virtual environment that is activated at the time you
install it. So this means that if we want to use our virtual environment we need
to activate it before we install or run any of the scripts in our project. The
way that you activate a virtual environment differs on windows and mac
and linux. So with mac and linux it’s the same way
but windows is slightly different. So I’m going to show you how you would activate your virtual environment if you’re using Mac or Linux
first and then I’ll show you how to do it using Windows as I am here. So the way
you do it if you’re using Mac or Linux is you type source env/bin/activate. Now this basically activates the environment
using the source that it will be available in this directory. So if you
created your virtual environment on Mac or Linux you will have a different set
of folders here than you would if you created it on Windows. So this is what the
command you run to activate it if you’re using Mac or Linux. This won’t
work on Windows because it doesn’t have the correct set of files. Okay if you’re using Windows like I am here the way that you activate it is you type
env scriptsactivate.bat this runs the activate batch script. Okay so once you’ve activated it in the
method that is appropriate for the operating system you are using you will
notice that you get this env prefix at the beginning of our command line
terminal. This signifies that we have enabled our virtual environment and we
are currently working within that virtual environment. So anything that we
install or any changes we make to our project will be applied to this virtual
environment specifically. Okay so the next thing that I’m going to show you
how to do is deactivate your virtual environment. So sometimes you want to
switch back to your operating systems route virtual environment or you want
to switch to a different virtual environment. To do that you simply type
deactivate from the virtual environment hit enter and you’ll notice that the
prefix with the environment name disappears. Okay so that’s how you use
virtual environments with Python. If you have any questions or you get stuck then
please leave a comment in the video below and I’ll try and help you out with
that. Please remember to subscribe to our
channel to get future updates of all the future videos that we publish on this
channel. Thank you so much for watching and I’ll see you in the next video.

Comments 5

  • Nice tutorial. Question though. So say when I activate my virtual environment, install my dependencies, and finish writing my script. Do I have to keep that virtual environment activate so my script can read those packages?

  • Hey thanks for making this tutorial, question: it was all going smoothly until I ran envScriptsactivate.bat – when I did that I got the error code "The system cannot find the path specified". Do you know what I can do to fix this?

  • Hi Mark my name is kany I live in London I have an app streaming idea which I have done servers for I am committed to my idea my only problem is I have disclosure and I am bad at maths I am willing to work hard

    But I need an app programme develop who understands programming is there anyway that we can get together and I can pitch my idea idea to you if you like it maybe we can work together

  • Hi, thanks for your tutorial! How can I run programs with third party packages in IDLE? Can I activate/deactivate the venv that I have created from the IDLE Terminal? I have been searching the web looking for answers for hours but I cannot find a satisfying answer…can anybody please help? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *