(train whistling) - Shocking development! I am adding another workflow
video, because every once in a while I have to download
a Python script and run it. And I do use lots of
different kinds of Python development tools from time to time. Jupyter Notebook is a wonderful
way of running Python code in an interactive way right
in the browser, and there's all sorts of other things you can do too, different ways to work with
Python, but I'm going to show you one little thing that I tend to use, which is that I often need
to run some Python script, to process some data, or
train a model or something that I'm going to eventually
come bring and use in JavaScript. And so the tool that I
have kind of landed on, that I like to use, is
something called Virtualenv. And Virtualenv, Virtual
Environment is a tool to create isolated Python environments. And this is useful because
I can kind of contain all the Python version,
and all the packages and all the dependencies for one thing that I'm trying to do
within one workspace. And again, there's Docker,
and there's so many other things you could use, so I'm just going to show
you Virtual Environment, 'cause it solved a lot of
problems for me when I get totally lost in my Python
installation on my computer. Now, one thing, to get started with this, you will need to have
Python on your computer and PIP on your computer, so
those are two prerequisites. Installing Python, installing
PIP, if you're on a Mac, it most likely came with some
version of one or the other. And the other, so you're
kind of in good shape, and that's what I am here, so
the first thing I want to do is, I actually just want to say pip install, PIP is Python installation packager. Ha, that's not what it stands for at all. But it's your package manager for Python. Sometimes you'll say PIP
three if using Python 3. pip install virtualenv,
Virtual Environment. So I want to install that. Truth of the matter is,
I've already installed it, so there it is, and you
can see it's kind of part of my default Mac OS Python
installation, so maybe you're using Miniconda
or Anaconda or some other but you want to first start
with Virtual Environment. Now, once I have that installed,
I need to find a place on my computer to set up
my Python environment. And it could be anywhere, I
could put it on my desktop. It's crazy, I am a crazy
person who sometimes keeps entire Python environments on my desktop. Watch, I'm going to do
that right now, cd desktop, I'm going to look on the
desktop, there's nothing there, there's like a folder called
website, there's processing dot app, and then, I'm
going to go here and, user guide is probably what
I want to take a look at. I want to create a virtual environment. This is the, this is
really the only command, well, this is one command,
you don't even need to memorize it because we can look it up. Virtual Environment ENV,
now, this is not meant for you to just have to
type, capital E, capital N, capital V, that is a
placeholder for the name of this Python workspace
that you want to use. So I'm going to grab this,
I'm just going to say, And I'm going to say, virtualenv
and I'm going to call this coding train demo, coding train dash demo, that's going to be the
name of this workspace, and it is doing all sorts
of stuff to set it up, and blah blah blah. Now I can navigate to the
desktop and look, there it is, right there, I've got
processing, website directory and coding train demo, and an
entire Python installation, with everything I might
ever possibly need. Now, it's Python 2.7, so,
I'm going to at some point have to examine how can I get
a different version of Python if I want with Virtual
Environment, which shouldn't be a problem, but let's just push forward. So once I've installed
a virtual environment, once I've created a virtual environment, I need to activate it,
so the way to activate it is by running this command,
source bin activate. So in other words, inside of
here, inside of bin activate, this shell script will turn
on this virtual environment. So right now, you can see,
if I'm in the desktop, and I just say which
python, it's pointing to user bin python, the default
MAC OS Python installation, and if I say, python dash dash
version, we're going to see, I'm using Python 2.7.10 which
I, admittedly, is a very old version of Python, so I'm going
to, by the end of this video, I'm going to be using Python 3 (laughs) So now, if I were to say source, coding train, oops, coding
train demo, (laughs). Oh no, I've got to tab through all of this 'cause I'm a lunatic. Coding train demo bin activate. All of a sudden you can see, look at this, this little note, in my terminal window, in my console, is letting me
know that coding train dash demo is activated, so
if I were to now say, which python, you can see
this is the one it's using. Users coding train desktop
coding train demo bin python. Now again, do I really
want to put this stuff on the desktop, probably not,
but I can move it around. Everything to have to do
with this version of Python is running right there,
if I can say, I can say python dash dash version,
and it's still 2.7.10 but now let's say I want
to install TensorFlow. If I say pip install tensorflow (singing) ♪ PIP install TensorFlow dance ♪ And it's finished, oh, and
it's installing some other dependencies (laughs),
it's going to take forever. Why did I pick this example? (train whistling) (singing) It's going to speed this
up, ah, okay, it's done! Alright, so it finished
installing TensorFlow, and if I'm just going
to run Python right now. I can say, import tensorflow just to see and I get no error, meaning
TensorFlow is properly installed for this environment, and how do I get out of this? Control C, no, exit,
something like that, okay. Now, if I were to go to
a new tab, right, look, coding train is not activated,
I'm in a different Python. I'm not in my virtual environment. If I were to say python, and I
were to say import tensorflow it has no idea what that is, but I can say source I got to go to coding train bin activate and now, now that workspace
is activated so I can install a whole lot of packages
and they are all here coding train demo, lib,
Python 2.7 and site-packages and we can see, aha, look,
these are all the things that were installed, including TensorFlow. So everything is contained
within this one directory and I'm working with it here right now. Alright, so now, what if
I want to create a virtual environment with a
different version of Python? So first what I want to do to
make a new virtual environment is I want to deactivate
the one that's going. So I'm just going to write deactivate and then I can just check
where I am, here I am on the desktop, so I want to make
a new virtual environment but with Python 3, so I
can say virtualenv dash p for the version of Python,
Python 3, and I'm going to call it coding train dash 3 (gasp)
aaaaahh, Python 3 does not exist, so the problem is
Virtual Environment, it's not magically creating a workspace
in Python out of thin air, it's actually taking it
from the existing default installation of Python, so I
actually have to have Python 3 installed on this computer
to be able to make a virtual environment with Python 3,
so there's so many different ways you could install Python 3. I kind of like to avoid using
this tool called HomeBrew, which is a package manager
for installing Unix utilities, it's written in Ruby on a
Mac, but the official Python docs say that's the
correct way to install it, so let's do that, so
I'm going to grab this. I'm going to install
HomeBrew, which I probably, let's see, do I have brew installed? No, I do not, so let's install that. Install this thing called
HomeBrew, and I'm going to enter in my password, and I'm
going to enter it in again, and I'm going to try a
different password, there we go. So this is going to
install for a little while. (electric music) (bell ring)
Oh, it finished, okay. So I've got HomeBrew, looks
like it was installed properly. That's great, so now I'm going to, I think the path, let's see if the path, so remember, if I'm going to type brew, ah, it's already there,
great, so brew got added to my path already, and
remember if you've been following my workflow videos,
I'm not looking in the dot profile, I'm looking
in the settings file, it's like dot shrc, zshrc, that's my, so let's look at that. I can say, I can open code dot zshrc just so we can always have a look at this file and we can see, ah, so it probably, HomeBrew was
just part of the main path so it looks like we're okay, alright. So now, I can go and
try to install Python 3. Brew install python,
we'll see what happens. Like that, and now we're going to have to wait a little while again. (electric music) (bell ring)
Okay, so, great. So that's done, now, if I say which python user bin python, which python3 user local bin python3, so
I have now have Python 3 installed on this computer, and I can, and let me just say python dash version, oh, no no, sorry, dash dash version, (laughs) shoot, python dash dash version, and then python3 dash dash version, great. So look, I have both versions
of Python on my computer. Now, here we go, remember that command that I was trying to type earlier? Virtual environment dash
p python3 coding train 3. Here we go, and going to set
up my virtual environment. I'm going to say source,
coding train, oops. Coding train dash 3 bin activate. Now which python, bin python, python dash dash version. Python 3, so in this case,
right, let me just show you that again, Python, the word
Python, is tied to Python 3.7 When I'm not in a virtual
environment, I need to specify Python, which is the original
Python that came with the computer, an old
version, 2.7, or Python 3, which is the new version
which I just installed, but since I created the virtual
environment with cloning Python 3, the keyword
Python is tied to that one and I can once again do
things like pip install numpy. Let's pick something that
hopefully won't take as long. NumPy is a math library
for Python that's used for matrix math and sorts of other things. It installed that, if I do,
if I say python import numpy, we can see, I'm just sort
of checking, it's working. I'm going to exit from here,
I'm going to deactivate and I'm going to say python3 import numpy. Oops, I can't spell, import
numpy, not there, okay? So we can see, these
workspaces, one is Python 2.7 with TensorFlow and one is
Python 3 with NumPy installed and I can completely
treat these separately. I can, and they're just
sitting here on the desktop, which is a very weird place to have them, but if I decide, you know
what, this whole workspace is worthless, I'm just going
to delete it, and it's gone. Okay, what are some other
things I want to show you? So one thing is, you're going to
find it hard, you're going to, this is going to happen to
you if you're using Virtual Environment, you're going
to forget to activate it, and you're going to start
doing stuff and running Python scripts and you're like,
why is it not working? Well, so one thing is it's
helpful to have that reminder. Again, I want to say a few things, one is, I don't want to have to
type this every single time. Maybe there's a way I can
create a quick alias to that environment, and in fact there is. So I'm going to go back to my
zshrc, that's the equivalent of the bash profile,
this is where are of the, shell settings are, for the,
when you start up my shell. So I'm going to go to that, I
have it open in Visual Studio code and I can just create an
alias by saying something like So, I'm going to do an alias
for my virtual environment, and I'm going to say, alias
coding ct three, like just quick ct3 is my alias for
Python 3 and then I can say, I put in quotes, source
activate and then I need the full path, so I need to
say users, actually I might be able to do this, home. Right, home, desktop,
I don't know (laughs) Let's go to here and
say, let's just use this. There's probably a way to
use an environment variable, but basically what I'm
trying to do is do this. Users coding train desktop, and again, you probably don't want to put your virtual environment on
the desktop (laughs). Coding train 3 bin activate Actually, scratch that,
before I go to the shell, this is never going to work
because I forgot something very important, I have to
actually set this alias equal to that command, so
if I put an equals here, then I go back to the
shell, I should be able. Now, if I just type ct3, it's
not going to know that command. I could restart iTerm and
it would probably know it, but I can also just refresh
the shell by saying, source dot zshrc and now
I should be able to type ct3, ahh, oh, no such
file directory activate. So at least it was
looking for that command, it ran that command, why did it not? Oh, I don't know what I'm,
I don't know why there's an extra activate here,
this is what it should be. Source, the full path, to bin activate. Okay, save, update, and there we go. So that activates it and
then I can say, deactivate, and ct3, there we go, now
something you might want to do, although you have to
be careful about this, is what if I actually just
want to automatically, every time I open a new
shell, I just want to activate my virtual environment? So in fact, one thing I could
do is, once I have this alias, I could actually just say, hey,
the last thing that you do, after all the setup happens,
is just, I'm going to go down to the bottom, there's tons
of stuff commented out. Just run that alias, ct3, so now, if I quit iTerm, quit,
and I just restart it. You're going to see, hey look,
coding train 3 is activated. A new window, coding train 3 is activated. A new tab, coding train 3 is activated. So coding train 3, that
virtual environment, would be auto activated every time. Typically, this is not
what I do, I'm sort of, like to turn it on and off,
now you can see it's not there, but I can just type in
my alias, and I'm running my virtual environment, alright. So this is pretty good, now
you can do all of your Python stuff in this environment,
and also, it's safe. Any other, as long as
you're, if you deactivate it and start running all sorts
of other installation stuff, and other Python stuff on your computer, it will not affect that
virtual environment. So if you know you need to
have exactly this configuration and stuff for this one particular
project, you can sandbox it and not have anything else affect it. One more thing I'd like to mention. So I know I started with pip
install virtual environment, that's what I was kind of
used to, and that's what I've always been using, but it
is actually true that once you have Python 3, the whole
idea of virtual environments is built in to Python 3 and
as of Python 3.6, the way to run, to create a virtual environment just with Python 3, is by
saying, so first of all, you have to have Python 3
installed, which I have done as part of this tutorial,
but now I can say, python3 dash m and the
command I want to run is venv, Virtual Environment,
and then, oh actually, and let me be more thoughtful
about where I'm putting this. I'm going to go to the
documents directory, and then I'm going to say python3
dash m venv and I'm going to call this coding train demo again and I have now created
a virtual environment which I can see if I go to the finder. I can see, there it is,
coding train demo again. There is all the Python stuff, Python 3.7 and now I can activate
it by saying, source, coding train, oops, coding
dash train demo again, bin activate and now that
Python's environment is activated. Just to be sure which Python,
yes, in fact it is this one. I could alias this one,
I could install stuff, I can deactivate it, so you
could say I could deactivate this and I can reactivate my
other virtual environment. I can deactivate that, I can
go back and activate this one. I can install packages for
this one, and deactivate and go back to the other
one, I could alias this one. All of the possibilities that
I have shown you are here. So, to recap, if you are using Python, and by the way, my most
recent, it's a guest tutorial from Yining, for training
a machine learning model to do style transfer, you
can go watch those video tutorials, this is a way
you can set up Python to run locally, training
your model on your computer. So, to recap, Virtual
Environment is a way of creating a sandboxed Python
environment on your computer that is not affected by
anything else and is just a folder anywhere on your
computer with all of the, everything Python and all the
dependencies you're using. In order to create a virtual environment, you have to have Python
installed in the first place. So I recommend you install
Python 3 on your computer. I showed you some steps
for doing that with brew, but if you're on Windows or
Lenix, there's other ways to install Python 3, and
actually, built in to Python 3 is the virtual environment
command, so you can just say, python 3 dash m v environment,
venv, and then name your virtual environment,
activate it, deactivate it, and enjoy your Python life forever, okay? So, I look forward to hearing
what you think (laughs) I'm really not a Python
expert, you might have noticed. I barely use it, so
hopefully this was helpful. I'm sure some Python experts
out there will tell me all the things I could do
better, or different ways. I look forward to hearing
about those too, okay? Goodbye, and I hope you enjoyed this extra bonus workflow video. (upbeat music)