How to Setup a New Project with Python Env

I’m writing this content to help you understand the best way to setup a new project with Python and using virtual environments properly.

An illustrative image for how Virtual Environments works

The fact is when I learned about Virtual Environments I didn’t realize how they could be used in a way that would help to scale and maintain my projects. If they used right it is a true powerful tool and it could help you to understand some concepts to isolate tools used in projects and never more feel lost with the packages you are using for a specific project. Why is that?

When you create a virtual environment and activate it, when you install packages that have the tools you need for that project you are installing only inside that environment and by doing that you are isolating the project and if you need to run that project on another machine you can trace all packages you need for that project and install in an automated way.

But enough talk, let’s setup a new project:

  1. First create a project directory/folder on you machine. I’m going to guide you through Linux commands (I’m using two commands with &&, that is used to run a command after another successfully, the first command creates a directory on my current directory and the other changes current directory to that one created):
mkdir myproject && cd myproject

2. Create the virtual environment inside your project directory, with some obvious name (in this case I’m calling “env”):

python3 -m venv env

3. Put the directory of your virtual environment to “.gitignore” file. This would be useful to when you add your project to git, the packages wouldn’t be added to the repository, maintaining your repository clean of junk.

echo 'env' > .gitignore

4. Activate the environment (and every time you will work on that project you have to do this step to work inside your environment):

source env/bin/activate

5. Now when you install a package it will installed in your environment:

pip install django

6. Freeze the requirements. Every time you install or remove packages from that environment you should record that to a file that stores all packages necessary to that project (this is the file you should add to your git repository for that project):

pip freeze > requirements.txt

7. If you need to install the requirements for that project you would use that file created before:

python -m pip install -r requirements.txt

I’m new to blog posting so if you have some tips or contributions for me to grow in this business I appreciate. My mission with this is to help everyone with possible struggles we all have in the beginning with software development and some other IT stuff. So if you have anything that you wanted me to write about it I’m open too. Thank you for reading this and I hope that it helped you.

Recent graduate in Computer Science. Love to design code with best practices and solve problems.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store