How to Install and Work with Pipenv in Linux
Now that you have virtual box and Linux working on your system and you have some form of a text editor if you went through the Sublime Text installation guide then you have sublime text but you can use anything that you want for this course. In this guide, we are going to install Python on our system.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now I want to show you something so if we click on terminal here then I'm going to have a nice little surprise for you. And that is you actually already have Python installed on your system. It's one of the things that ships with the latest version of Ubuntu. So if you want to test it out there are a couple of ways. You can say python and if you just write Python period like that it is going to give you the basic 2.7.12 version.

large

But if you say python3 then it's going to give you Python 3.5.2.

large

Now want I want to do though is show you how to use what is called pipenv in Linux and so pipenv is a way that you can use multiple environments and so there are a few ways that we can get this. And let's just see if we have pip installed so I'm going ot say pip --version and it looks like we do not have it.

large

Let's run this installation command so I'm gonna say sudo apt install python-pip and so to type in your password. Oh, and it has an error.

large

I'm actually glad that this came up, I've had this happen a couple times. if you get this error where it says could not get lock and get this file it's temporarily unavailable. Typically that means that there is some background process that is running and there's a pretty easy way of being able to fix that. And that is that you simply reboot the system so let's reboot this and that's going to go and it's going to just reboot and restart our virtual box, start it back up and then it should work and I am glad because this is the first time I got that specific error and I'm glad it's on there so that you were able to see what the fix is.

It's pretty easy to just restart your system and it will work so as us loads up let me talk a little bit about what pip is. Pip is a package manager and it allows you to install outside dependencies if you've already gone through one of my Python courses then you may have already used pip so that is good. This is the way we're going to install it here so I'm going to now say sudo apt install python-pip type your password again and now you can see that it's working. It's going to ask if you want to continue.

large

It's going to go out and install all of the dependencies so that we can use pip to install outside Python libraries and the first one that we're going to install is going to be the pipenv library and so that is going to allow you to have multiple environments on your system and then any dependencies that you have you're going to be able to nest those inside of a specific version of Python. So here I can now say pip install --user pipenv. And this user flag means that we're going to use the user's settings permissions and passwords and those kinds of things and so this is the cleanest way of running this installation.

So it's going to go and install this for us. It looks like everything here is working nicely.

large

And there we go, looks like we are. Now let's see if we have to restart the terminal or not, we can test it out by just typing pipenv right here and as long as you don't get an error message saying something like we couldn't find the command pipenv then you should be good.

large

So what pipenv just by itself will do is give you a full list of commands. And so it gives you some helpful usage examples I definitely recommend that you go through these and then it has a list of some of the options here.

large

So let's actually test this out to make sure it's working. I'm going to create a directory here so I'm going to say mkdir and we'll just say python-projects and let's change into that directory. And so the way that you can use this and get it started up is by saying pipenv. Oh, and before I do that let me show you how the current state of the system. So if I typed Python right now like I showed you it's using the default 2.7.12

large

Now if you want to use a different version than what you can use is pipenv and then say pipenv --three. And what this is going to do is it's going to create a virtual environment for the project and as you can see it went and it found Python 3 on our system and now that's what it's using.

large

So it shows where that virtual environment is and it also created a pip file for us so if I go to pip file you can see that we have the url and it's just using the basic version of the pypi store. It's going to bring everything in via SSL and then you can see that we do not have any dependencies because we haven't installed anything yet but it does show that we're using Python 3.5. So if we install something else on our system like Python 3.6 or something like that then we can update this file and that's what it will use.

large

Now if I close us out what I can do now is make sure it's working by installing some outside library so lets install the request libraries. So I'm going to say pipenv install requests. And if everything's working it should go out, grab the request library and then bring it back down to us. So far it looks like it's getting the different library components, it's installing them, it's giving us our cool little snake Python icon and it appears like everything here is working.

large

Let's take a look at our pip file now

large

and there you go, look at that. We have here this request library that is available for us to use now. So now if I say pipenv shell, what this is going to do, is startup that virtual environment and you can tell from where we're at right here on the shell command line when it says this Python projects and then this weird little hash what that means is we're now inside of a pipenv shell session.

large

Now if you remember back when I typed Python before and how it automatically used 2.6 or 2.7 whatever the default is on the system. Well now inside of the shell when I type Python it's going to use this virtual environment. So now you can see it says Python 3.5.2 so it's using the project's version of Python instead of using the systems.

large

There are a number of benefits to going with this kind of a process. And one of the top ones being that now you can have different versions of Python for different projects. There are many times where I will go and build out some kind of Python project and then a couple of years later I will come back to it and if I've upgraded my version of python on the system and that previous project just used the basic default system version of Python then I'm probably going to run into some errors especially if there were significant version changes in the language. And so what using a tool like pipenv does is it allows you to nest and lock all of your dependencies on a project by project basis.

If you are a Rails developer or a javascript developer then this process will most likely be a very familiar one to us. In rails, the rails, projects have a concept of a gem file where you can lock your version of Ruby in it's case along with all of its dependencies in one file and then in JavaScript they have what is called a package json file and that's where you can lock all of your dependencies and list them out right there.

So with python and pipenv, we're able to do that. We're able to have one single file for our project our Pip file and that is going to store all of our dependencies, our version numbers, and anything like that so that we can switch from project to project and not worry about our different versions clashing with each other. So this is definitely considered a best practice in the Python community.

Now if you have your virtual environment setup and you went through all those steps you now have the ability to create virtual Python environments inside of Linux. And now the last command I'm going to give you is control + d that will end that shell session. And now if you type Python you can see we're back to using the basic system default.

large

So great job if you went through that you now know how to install pipenv and a different version of Python on your Linux system.