Hey there. In this video I'm going to show you how to install and use PyEnv on
your machine. Before I go ahead and show you how to install and use PyEnv I'm
going to explain a bit about what PyEnv is and what it's useful for. PyEnv is a tool
that you can use to easily install and switch between different versions of
Python on your development machine the reason you might want to do this is
because if you are a freelance software developer like myself you may work on
many different projects for many different clients in quite a short
relatively short space of time that means that you may be working on
different projects that require different versions of Python for example
I worked for a client previously about a year ago and they were still using
Python 2.6 on some of their projects now this is because they had some legacy
applications and those legacy applications had not yet been ported to
a later newer version of Python they were still using the old outdated
version of Python 2.6. Now I work for a new client and fortunately they use the
latest version of Python 3 so it's useful for me to be able to switch to
and from these versions of Python on my development machine. So let's go
ahead and set up PyEnv on our machine here ok so I have a linux mint desktop
here and you can install Python or you can install PyEnv on Linux or Mac OS okay
so if you click the link in the resources of this video it's going to
take you over to the PyEnv github page this is basically the home page for
PyEnv and we're gonna scroll down and what we're actually going to do first is
we're gonna click on the wiki link here and then in the wiki
we have an option called common build problems now the reason that I'm doing
this first is because every time I have installed PyEnv I've had to go through
and install these dependencies first really in my opinion they should be in
the installation documentation of PyEnv but they're not and it can be quite hard
to debug the problems so you all I do when I'm installing a new PyEnv
instances I just go over to this requirements page under common build
programs and I copy this list of requirements and make sure I have them
all installed on the machine so if you copy that and again if you're using Mac
then these are the requirements so you just use brew to install this
it's just readline xz if you're using Ubuntu or Debian like I
am here Linux Mint which is a Debian based operating system
then you need to install all of these dependencies so let's go ahead and paste
that into our terminal window hit enter I'm going to put in my password whoops
typed it wrong there you go and now it's going to go and install all of these
requirements on the system it should take just a few minutes to go and
install them and while that is installing we're going to
go back to the PyEnv github page and we're gonna go back to the home page
so click on the home page and scroll down to the installation section of the
github instructions here and this basically tells us how to install
PyEnv and if you're on Mac OS then consider using homebrew but I think
these steps that I'm going to show you using the PyEnv installer should
also work on a Mac but I guess it's quicker and easier if you already have
homebrew set up to just use homebrew so feel free to do that if you're using Mac
I'm going to go over to the PyEnv installer and I'm going to install it
using this script that they provide in the PyEnv installer project ok so we just
need to wait for these dependencies to install probably another minute or so
left on that it's just setting up and configuring your dependencies now and it is
complete another thing we want to make sure that we have installed before
running this command is the cURL application now I'm pretty sure it's
already installed on my machine but just in case it's not installed on
yours you can type sudo apt-get install curl and hit enter. Yep I was right and
it already is installed to its newest version on my machine so I'm good to go
and copy this line of code here or this command and paste it into my terminal
window and hit enter now what this does is it downloads the installer script from the
PyEnv project and then it just runs it in our bash window so it runs all the
commands which then presumably will go and download all the requirements and
install PyEnv on our system ok so that installation happens pretty quickly and
once it's done it will give you this screen where it has
warning seems you still have not added PyEnv to your load path now this is
because we need to add these lines here
like it says we need to make sure
that these are run each time our terminal window loads up so the way that
we do it is it says here added to the bash profile but the bash profile doesn't
exist on my system so I'm going to add it to the bash RC file the way you
do that is you open up the bash RC file like this so I'm going to use atom to open
in it but feel free to use VI or nano or whatever your favorite editor is and
this just says from the home location the squiggly line slash then open up
the .bashrc file we're gonna hit enter there and it may take a minute or
two for it to load up our atom editor this is actually why it's a lot
quicker of use VI or Nano I just use atom because it's kind of easier to see
what's going on when demonstrating in the video now I'm
gonna close off the tree view here and we want to scroll right to the bottom of
our bash RC file and add a new line and I'm gonna start by adding a comment by
typing the hash symbol and the comment is going to be PyEnv configuration this
is just so I know that this block that we're going to be pasting in now is for
the PyEnv configuration so then you can paste these three lines in its export
eval and eval and then you can make sure you save the file so I'm gonna push
ctrl s just make sure that your file is saved now what this does is it just runs
these three commands each time we start a new bash session or each time we load
the terminal up the bash rc file is the same on Mac and Linux so you should
be able to add it on your Mac as well in the same fashion but this now means that
PyEnv is configured if we type PyEnv in the terminal you're going to see that
it tells us its currently not installed now this is because this only runs when
you first load bash so because we updated this while we had our terminal
window already open we just need to close the terminal window and load it
again just so it runs
these commands alright now that it's done if we type PyEnv again it should give
us a list of possible PyEnv commands and this shows us that PyEnv has
been installed on our machine correctly okay so the two main things that you
want to do with PyEnv is you want to install different versions on Python and
you want to set the default version that the system uses to one of the versions
that you have installed okay so the way you install new versions is first we're
gonna list all the available versions that you can install through PyEnv and
we're going to do that by typing PyEnv install list with two hyphens before it
that's the list switch hit enter and what this will do is it will list out
all of the different versions of Python that are available on
the system you can see that there are loads of different variations of
different versions of Python so this is where the benefit of PyEnv comes in
because even if you had to just manually install one of these it would be
difficult because you'd have to locate the version you'd have to download it
you may even have to build it from source PyEnv does all of that for you so
let's locate the one we want now I like to use the latest version of Python
Python 3 but I don't like to use the development version because I only like
to use the stable version so the one that I always install is
Python 3.6.2 now this is the latest stable version of Python 3 at the time
of recording this video so all I'm going to do now is type...well next we're going
to install this version of Python and we do that by typing PyEnv install
3.6.2 obviously you could replace this with any version you like
and then hit enter now this can take some time because what it does is it
downloads the version of python from the python.org website so it downloads the
version that we need and it installs it but when it installs and actually builds
Python from source so because it builds it from source it's actually quite a lot
of processing that it needs to do it builds the whole Python all the binary
and everything from source and that's why we needed all of those dependencies
installed before we started because those dependencies that we installed
from the PyEnv page I'll just take you back there to show you to refresh your
memory these dependencies, under wiki common build problems these dependencies
all of these are required for building source code from source so this is all
required if you want to build Python from source then you need
to have all of these here so that's why it takes quite a while
to install these new versions so I think we're going to cut the video there and
then we're going to come back to it once this version of Python is installed
Ok so now that the version of Python has been installed we're going to go and run
Python just to see which version it defaults to so it currently defaults to Python 2.7.12
which is the version of Python installed by default when we install
Linux Mint we want to switch to Python 3 and we want to make sure that every time
we run a script it runs it using Python 3 the way that we do that is we need to
set the Python version in the version file in the PyEnv directory so let's open
up that file by typing atom ~/. pyenv/version
now remember it needs to be version singular and this file doesn't actually
it doesn't already exist at the time of installing PyEnv we're going to be
creating it right now so I'm going to type that and then it's going to open it
up here in the atom editor and all you do is you specify the version of Python
you want to use as your default version no spaces no Python text or whatever
just 3.6.12 which is the version we installed save that
head back here and now type Python again and you see it will try and load that
but it says that this version is not installed because it's not 3.6.12
it's 3.6.2 you probably spotted that when
I made the mistake so let's just correct that save that and retype python and
there you have it it comes up with Python 3.6.2
now if you wanted to install another version of Python and you want
it to switch to that one you just type PyEnv
install list find the version that you want to install I don't even know what a
lot of these versions are (miniconda) I've heard of jython before but I've
never used it but you just scroll up and you find the version that you want
and you type Python install and that version and then you just change this
version file to the version you want to default to so that's how you install and
use PyEnv on your system I hope you found this video useful if you have any
questions or feedback or anything please feel free to leave a comment in the
video below and please subscribe to my channel for updates and new videos and
things like that thank you so much watching goodbye