PythonSQLAlchemySQLitePostgreSQL

SQLAlchemy Installation and Setup

A friendly witch stirring a cauldron and placing herbs and spices into it.

Creating a python virtual environment

If you already know about virtual environment you may skip to the bottom of this chapter section.

What is a virtual environment

Virtual environments are containers for python program execution, by using a virtual environment you make sure that the dependencies for your applications are not overlapping, meaning you can install program specific packages, this also allows us to easily deploy our applications, by making sure that all of our dependencies are specific to a virtual environment, we can make sure that moving our applications from our personal computers and reinstalling them in a production environment is as smooth as recreating the virtual environment in a different location.

Creating a virtual environment

Creating a virtual environment is quite easy, but we do need to remember to reuse it whenever we work on our application, thankfully IDEs / Test editors with python support can be setup to reuse a virtual environment, or at-least to select a virtual environment easily.

If you have both python and python3 installed on your system:

python3 -m venv venv

If you only have python3 installed on your system, it might be accessible through

python -m venv venv

You may change the second parameter venv with the name of the virtual environment you would like to give it.

After running the command you should another folder in your project folder called venv .

Activating your venv

After creating a virtual environment we should also activate it, meaning that whenever we use the same command line instance we used to activate the environment, any package we install will be added to the virtual environment we have active.

source venv/bin/activate

Since a command line holds its state temporarily, whenever we create a new one it gets reset to it’s default state, which means the virtual environment is not going to be active, make sure that your virtual environment any time you wish to install a new package through the command line.

Installing Dependencies

Create a requirements.txt file

The requirements.txt file lists all of the dependencies we would like to install in our virtual environment.

SQLAlchemy>=2.0.37
psycopg2-binary>=2.9.10

Installing our dependencies

To install our dependencies we need to point the installer to where we’re holding our dependencies, using the pip command we can do just that

pip install -r requirements.txt

This command uses the install option with the -r flag to specify our requirements file.

In the next article we will begin by creating a connection and writing our first queries.

Author image
I'm Yonatan Vega
Owner & Creator, Fullstack.rocks
I've enjoyed writing code ever since I discovered it was an option. At a young age I started by writing code for MMORPG's private servers, and hosting my own private servers at home.

Today I'm writing and architecting software using many different technologies, and I'm always looking for the next thing to learn.