From the course: Secure Coding in Python

Installing pipenv, Python, Django, Flask, and Django REST framework - Python Tutorial

From the course: Secure Coding in Python

Start my 1-month free trial

Installing pipenv, Python, Django, Flask, and Django REST framework

- [Instructor] When we run a Python program, we want to know precisely which distribution is run. We also must keep clear tabs of our dependency management and isolation. This way, when we import an open source package such as a web framework, we know precisely which version of that package we're using. This is imperative to keeping our application secure. Why? Well, that way we know we're up to date on the latest and greatest security patches and fixes. Pipenv is a great tool for installing packages and managing dependencies. Using pipenv lets us comfortably isolate our Python projects. Further, we may use it for preliminary security checks. So let's go ahead and install pipenv. So here I am at my exercise files at 01 01 02. And before I do anything, I'm going to install pipenv. Since I'm using Mac, I'll be using brew. But if you're using a different operating system, go ahead and check what's a comparable package manager. So it's important to type this carefully brew install pipenv. And I'm told that it's already installed and up to date, I'm going to clear my terminal. Keep in mind, this might take a few minutes if you don't have pipenv installed on your machine. So now let's type in ls to see what's here. So here I see two directories, feed and status. These are APIs that are intended to power a small microblog. You can think of them as APIs for tiny Twitter or LinkedIn status feed. So the main difference between feed and status is that feed uses Django while status uses Flask. So I'm going to clear my terminal and I'm going to cd into feed. Clear. And I'm going to type in ls and I will see that there is a pip file here. Now, if I type in cat Pipfile to take a look inside, I'll see that this is a specification for my environment that has not yet been built. So under packages, I'll see Django REST framework and Markdown and Django filter. Great, clear. And the magic of pipenv is that the moment you have this pip file, all you have to do to create or recreate an environment, is to type in pipenv install. So it's telling me that it successfully created an environment. Clear, great. So what if there were no pip file here? Well, no problem. All you have to do is install your dependencies and pipenv will magically create a pip file for you keeping track of your dependencies as well. So let's go ahead and try installing something that's not specified in the pip file. So I'm going to do pipenv install requests. This is one of my favorite Python libraries created by Kenneth Reitz who created pipenv as well. Clear my terminal. If I take a look at the pip file by typing in cat Pipfile, I'll see requests has been added to my packages here. Clear my terminal. And since I don't need to request lib for this project, I'm going to hit the up arrow, and I'm going to add un, so pipenv uninstall requests. Clear my terminal. And if I type in cat Pipfile, request is gone. This saves you a ton of work as far as keeping track of your environment and dependencies. So clear my terminal. And I'm going to quickly type in pipenv graph. And I'll see a graph of dependencies and I'll see pytest Django that has pytest, and Django REST framework that has Django as a dependency. I'm going to clear my terminal. Now remember, this is feed. This is the Django project. My Flask project is not yet set up. So for that, I'll have to cd into dot dot status clear. And if I type in ls, I'll see a pip file here as well. Now if I do pipenv graph, there is no virtual environment here yet. So clear my terminal, and take a look at my pip file. And I'll see Flask and Flask related packages. Clear again. And finally hit pipenv install so that an environment may be set up for this project. So now by typing in pipenv twice, we've basically set up two separate environments. And this is the important part. Throughout this course, I've created environments per chapter. So when you use these exercise files, simply cd into the chapter and do pipenv install and within a few minutes, you should be set to go. Clear, and if I pipenv graph, here are my dependencies for the Flask project. Clear. So if this were a cheesy commercial or infomercial, I would say at this point, but we're not done yet because pipenv comes with a neat little security feature that I want to show you in the next video.

Contents