Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
An introduction to SQLAlchemy, the Python ORM.
Pre-Requisites
Docs
It's always a good idea to bookmark the documentation for reference. SQLAlchemy Documentation
Popular IDEs
-
Visual Studio Code and their Python language support
- Here are a lot of the extensions I am using: Meg's Favorite Visual Studio Extensions
- PyCharm (This automatically creates a virtual environment for you when you start a new project)
Local Setup Steps
- Create a folder for your project
- Open your folder in your IDE
- Open the terminal in your IDE
- Install virtual environment
- Mac:
python3 -m pip install --user virtualenv
- Windows:
py -m pip install --user virtualenv
- Mac:
- Create a virtual environment
- Mac:
python3 -m venv env
- Windows:
python -m venv env
- Mac:
- Activate your environment
- Mac:
source ./env/bin/activate
- Windows:
.\env\Scripts\activate
- Mac:
- Install SQLAlchemy
pip install sqlalchemy
- Create requirements file
pip freeze > requirements.txt
GitHub Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Hi there, my name is Megan and
I'm a teacher here at Treehouse.
0:09
My pronouns are she, her, hers.
0:13
In this course,
I'm excited to show you SQLAlchemy.
0:16
SQLAlchemy is an O-R-M, or
object-relational mapper.
0:21
It's a Python code library that
transfers data stored in a SQL
0:27
database into Python objects.
0:31
You can then use Python code to create,
read, update, and
0:35
delete data instead of writing
straight SQL, which helps speed up
0:39
development time, especially for
developers who are not fluent in SQL.
0:43
And it can often make managing
data less error prone.
0:49
SQL stands for Structured Query Language.
0:54
It's a language for
interacting with relational databases.
0:58
Relational databases are structured like
a traditional table with columns and rows.
1:02
If you're not familiar with SQL,
1:08
I highly suggest taking a peek at
the prerequisites for this course.
1:10
I've also listed them in
the teacher's notes below.
1:15
This prior knowledge will bridge the gap
between working with the database using
1:19
SQL, and working with the database
using Python's SQLAlchemy.
1:24
If you would like to follow along
in workspaces, then go ahead and
1:29
open up the workspace
attached to this video.
1:32
In the console,
run pip install sqlalchemy,
1:35
hit Enter, let it download and
you're all set.
1:41
If you would like to follow along locally,
create a folder for your project and
1:49
open it in your IDE of choice.
1:54
I'm going to use Visual Studio.
1:56
Let me go ahead and
create our models.py file.
2:04
We'll need to download SQLAlchemy,
so we can use it.
2:08
But first,
let's create a virtual environment.
2:13
Create a virtual environment by
running python if you're on Windows,
2:21
and python3 if you're on a Mac like I am,
-m venv env.
2:28
And this will start your
virtual environment.
2:33
You can see it's asking if
I want to use it, I do.
2:39
And you see our folder
up here was created, and
2:42
VS Code also adds its own little thing,
you don't need to worry about it.
2:44
Okay, now that we have our
virtual environment created,
2:52
we need to activate it.
2:55
Now, if you're on Windows,
you need to write and
2:56
I need to make sure I get
my slashes correctly,
3:00
.\env\Scripts\activate.
3:02
If you're on Windows.
3:11
If you're on Mac,
then you need to write it
3:13
as source ./env/bin/activate.
3:18
And this is all in the teacher's
notes below as well.
3:24
So you can reference it at any time.
3:27
So go ahead and type that out,
whichever one you need, hit Enter.
3:30
And now you can see I have this env here,
3:34
because I am now inside of
my virtual environment.
3:36
Perfect, now we can install SQLAlchemy.
3:41
Same install, pip install sqlalchemy.
3:43
Awesome, and
then we can run pip freeze, caret or
3:51
arrow to the right, requirements.txt.
3:56
This is the file that will
be important for others.
4:00
And if I run it, it will create this file.
4:04
And if I click into it, it'll tell
me sqlalchemy has been installed.
4:07
And this is the version
that I'm currently using.
4:11
Our requirements file is ready.
4:17
If you want to create a repo for
this project and
4:20
others end up wanting to run your project
to check it out, they will need this file.
4:23
If you do wanna post this on GitHub,
then let's also create a gitignore file.
4:28
New File > .gitignore.
4:34
Inside, we're going to write env.
4:38
This will make sure that environment
folder does not get shared to your repo.
4:40
You don't want that to go up.
4:44
We can also do this, .vscode.
4:47
If you're like me and
you're working in vs code,
4:50
we don't need that to go up either.
4:54
That's just for us locally here.
4:56
There's also this .DS_Store on Macs
that sometimes gets pushed up.
5:00
You don't want those to go either, so
you can add that to your gitignore file.
5:06
And that should be good for right now.
5:11
The most important one is
your environment folder.
5:13
This has a whole bunch of downloads in it,
and anyone running your project only
5:15
needs the requirements file to download
those requirements themselves.
5:20
So we don't wanna send them all of
our files, they can download it for
5:25
themselves.
5:29
Okay, cool.
5:32
Now that you have either workspaces or
your local environment set up,
5:33
let's check our version
in the Python shell.
5:38
I'm gonna run a clear.
5:42
Then I'm gonna go python3 to make sure
I'm in the newer version of Python.
5:43
And then I'm gonna import at
the top sqlalchemy, awesome.
5:49
And then I'm going to run
sqlalchemy.__version__.
5:56
And awesome, it's the same version
that's in our requirements file,
6:01
but this is another way that you
can check your version number.
6:07
You can see I'm on version 1.3.22.
6:12
And your version may be different and
that's okay,
6:16
technologies change quite frequently.
6:19
So if you're on a newer version than I am,
6:23
make sure to check the teacher's
notes throughout the course.
6:25
This way, you get the most up-to-date
information to go along with your version.
6:29
We're all set, time to dive in.
6:34
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up