Jose: Hi guys and welcome back.
In this video, we're going to learn about
psycopg2 and psycopg2-binary.
These are the two releases of the psycopg2 library
that we're going to use to interact with Postgres
from within Python.
The what SQLite3 is for SQLite, psycopg2 is for Postgres.
It's essentially a Python library
that allows us to interact with the database
in an easy way.
SQLite3 is simpler than psycopg2,
because SQLite is simpler than PostgreS.
There's simply more things that we can do with psycopg2,
but many things will remain similar.
However, there's an initial hurdle when installing psygopg2.
Unfortunately this is a very unfortunate hurdle.
Something that puts off a lot of people
and that's that there's two psycopg2's.
There's psycopg2 and psycopg2-binary
and lot of people get confused
over which one to install.
A lot of people have trouble installing them.
So in this video, we just wanted to tell you
a little bit about the differences
and also which one you should install.
So, for most people,
especially if you're new to software development
or you're only just starting,
of if you're using a Windows computer,
you should use psycopg2-binary.
They are essentially identical,
however, when you're using psycopg2-binary,
you can run into some trouble later on
when you're doing some really advanced stuff.
Never install both.
So only install one of them.
If you install, say psycopg2
and then you find an error
and then you try to install psycopg2-binary,
make sure to remove,
completely uninstall psycopg2 first.
But again, I recommend installing psycopg2-binary
if you are slightly newer to software development
or if you're only just starting.
So what are the differences?
Well, the main difference is that you need to build psycopg2
and that means you need a lot of other programmes
to make it work,
because when you install psycopg2
what you get is the source code of psycopg2.
And that code, in order to actually make it work,
needs a lot of stuff from PostgreS
and from a few other programmes
in order to actually, you know, build.
And so that means it's a little bit more complicated to do.
You can run into some errors,
especially if you have any programmes missing
and also sometimes psycopg2
won't know where your programmes are
and you have to tell it where they are.
And all of those things add to some complication.
Psycopg2-binary should just work.
But again, like I mentioned when we do some really advanced
and specialised things, it can have some trouble,
but we're not gonna get to those things in this course.
You don't have to worry about that.
And also, we normally deploy databases to a server
on the internet,
we will learn more about what that means later on.
And when you do that,
you'll be able to build psycopg2 without trouble.
I also left many references for you to read
if you're interested.
They can be a bit technical,
but again, if you want to learn more about
why I'm recommending psycopg2-binary
and why there's two and so on,
then you can read those.
So to install psycopg2-binary,
I'm gonna show you how just now,
but essentially, you just find it in PyCharm
or you type PIPinstall
and then it'll work
but you do need to upgrade PIP and Setup Tools first.
And you also need Python2.7 or Python 3.4 or greater.
As with the rest of this course,
we're gonna use the latest version of Python.
All right, let's get to it.
All right, so like I mentioned,
since we're now using PostgreSQL,
and we're gonna start building larger
and more complicated projects,
we're gonna begin using PyCharm
and virtual environments to manage our projects
a bit better.
If you don't know how to get PyCharm or what it is,
or you don't know about virtual environments
and what they are,
I've left a couple of links
in the resources section of this lecture,
for you to read.
We start off by creating a new project in PyCharm,
and what we wanna do here
is we wanna make sure to find a folder
where we wanna put our entire project.
And then, just at the end of it,
add the name of our project,
for example, movies.
What PyCharm is gonna do,
you need to make sure that the movies folder
doesn't exist already.
What PyCharm is gonna do is it's gonna create
the movies folder inside this folder
and it's gonna put the PyCharm project files in there.
If the file already exists, then PyCharm is gonna complain.
Now, for the project interpreter,
we're gonna click on New Interpreter, using Virtualenv,
then we're gonna leave the location as the default,
pretty much and the base interpreter,
we're gonna select Python3.8
of if you're using a different Python version,
then the Python version that you want to use.
I'm gonna use 3.8 because we wanna use the latest version
of Python in this course.
Now, when we create a new virtual environment,
what that's gonna do, essentially,
is copy Python into this folder
and give us a new version of Python using Python3.8
that is a copy of the existing one.
But it's gonna allow us to install libraries in here
that don't affect or are not shared with
projects that run on the base interpreter.
What happens eventually,
is that you can have a lot of different virtual environments
each one with its own set of libraries installed
that don't conflict with one another.
For example, if two projects wanted to use,
you know, different versions of the same library
and things like that.
All right, so let's click created
and then we will land up with our project created,
PyCharm also likes to give us tips of the day and so forth.
You can read them to learn how to use PyCharm a bit better.
It's all well and good.
Now once we've got that,
we want to go ahead and update PIP.
PIP is used to install Python packages or libraries,
so we want to update it.
There are two ways of doing this
and one way is probably gonna be faster
and easier for you,
and that is if you go over to the settings.
Sorry, I went a bit quickly there.
To the settings here in PyCharm,
it's called Preferences on Mac.
Then find the project, project interpreter,
double click on PIP,
specify the version
and make sure to match the number here
with the one up there.
And then press install.
However, I've got a bit of a weird installation here
on my computer,
so this means this is not working for me at the moment,
I still need to debug it and figure out why not.
But this should work for most of you.
If this doesn't work though,
it's good that I have a problem,
because I can show you an alternative way of doing it
that will work.
If that doesn't work,
click on terminal
and then make sure that you've got this pair of brackets
with movies at the start.
That's gonna show you
that you are currently in a virtual environment
and anything you install
is going to be installed in the virtual environment.
And then we're gonna type easy_install -U,
sorry that's _install, -U, pip.
And that's going to update PIP.
Now, again, try to use the other method if you can
because it's gonna be easier.
So once we've go this,
we can go ahead and install psycopg2-binary.
So again, we can go over to the settings
or preferences on Mac,
make sure that we've got this PIP installed there
and we're going to simply click on the plus icon.
Find here psycopg2-binary.
Make sure to spell things correctly there
and just install the package like that.
Hopefully it'll install successfully.
If it doesn't, just leave a question
in the course Q and A
and we'll help you out with that
and press OK then.
Now to verify that it works,
we're gonna create a new file.
So we're gonna right click on our project,
that's the movies project.
Click on New Python File.
And then create it, for example, app.py
and here all we have to do
is type import psycopg2,
right click on app.py,
and run it.
Hopefully you get nothing.
No red angry text telling you there's an error.
If you do get angry red text,
then try to find out why that is,
maybe put the error in Google
and see if you can figure it out
because there can be a few things that happen.
But if you find that you can't figure it out,
leave a question in the course Q and A
and we can help you find a solution.
'Course, we don't know the answers
to every possible error that can happen
when installing psycopg2-binary
but we can help you out.
But, hopefully, you get no issues whatsoever
and everything works just fine,
then we are ready to start using psycopg2
to actually connect to a PostgreS database.
So, this was a long installation process,
but now that we're here,
all we have to do is import psycopg2
and then in the URL, enter your ElephantSQL URL,
so that's the entire thing that we got
from ElephantSQL, I'm just gonna bring it up right now,
So just paste it in there,
I know it's pretty long but don't worry about that.
And then what we wanna do is connect.
So we'll do something like connection = psycopg2.connect
and paste in the URL.
Psycopg2 is smart enough to extract a username,
password and host name and port name
and everything from this URL
so you don't have to do it yourself.
Now, in psycopg2, as we will learn in the next few videos,
we always have to create a cursor to execute a query.
It's not like SQLite where we can execute queries directly
on the connection and it creates a cursor for us.
We have to do it ourselves,
so we'll do cursor = connection.cursor
and then we can do something like
cursor.execute select star from users.
And then we can, for example, fetch the first row.
So at first user is equal to cursor.fetchone
and then we can print the first user.
Remember, always at the end, connection.close.
All right, let's run this.
Something important to keep in mind
when working with PyCharm
is that there are two main ways of running anything.
You could right click on the file and run it
and this is the sure-fire code you want to run.
And now the other way
is to click the play button here at the top right,
but then when you click that,
what you're running is this.
This here is a run configuration
and at the moment we only have one,
because we only have one file.
Whenever you run a file in PyCharm,
PyCharm will create a run configuration for you
and save it up here.
You can then select one
and run it with the play button there.
So here you can see that we ran all of this
and we get the output which is our user from our table.
If you're confused as to why we're importing psycopg2
and not psycopg2-binary,
it's because we're only ever going to install either
psycopg2-binary or psycopg2.
They both create the psycopg2 package
in our virtual environment.
They just go about creating it
and installing it in two different ways.
That's why one of them is easier to install than the other.
But in both cases, you end up with the exact same code
that we interact with.
All right, now that we've created a project
and tested that psycopg2 is working,
we're ready to start migrating
our movie watchlist project from the last section
over to psycopg2.
So to start, I would delete this file here.
So I'm gonna go ahead and do that
because we don't need it anymore,
and copy all of the movie watchlist code files
into this project so that we can start the migration.
All right, let's do that
and I'll meet you again here
in the next video.
See you soon.