PYTHON VIRTUAL ENVIRONMENTS (Beginner's Guide to Python Lesson 9)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 \scripts\activate.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.
Info
Channel: London App Developer
Views: 74,930
Rating: undefined out of 5
Keywords: Python, Beginners guide to python, Learn how to code, Coding tutorials, Python tutorials, PyDev, Python virtual environments
Id: 4jt9JPoIDpY
Channel Id: undefined
Length: 6min 35sec (395 seconds)
Published: Tue Dec 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.