B.2 Project management
B.2.1 Git
If you are creating your project directory from scratch, you will likely want to initialize a git repository in your newly-created directory:
> git init
You will also want a .gitignore
file for your project.
Here’s my .gitignore
file for the Dash demo app; I include some RStudio stuff for if/when I open the project in RStudio.
# RStudio stuff
.Rproj.user
*.Rproj
.Rhistory
.Rdata
# virtual environment
venv
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
You may wish to adapt the virtual-environment entry to your situation.
B.2.2 Virtual Environment
Python virtual environments are used to manage dependencies so that they are local to a project. This is a different from the classic R idea of having a single library of packages used for all projects.
The idea of a project-based is also used in JavaScript (e.g. npm, yarn) and is gaining popularity in R with the renv package. In fact, this book is build using renv.
The goal here is to show you how to establish and manage a virtual environment for a Dash project.
In your newly-created project directory, from the terminal command-line:
> python -m venv ./venv
This creates your Python virtual environment by creating a directory in the root of your project called venv
.
The name of the directory is determined by the last argument, in this case ./venv
.
There are a number of “standard” ways to name virtual environments; "venv"
is one of them.
It’s really up to you and your collaborators.
The important thing is to make sure that you have a .gitignore
entry for the virtual-environment directory.
Next, let’s activate the environment.
This tells your terminal that this is what you want to run when you invoke python
.
In your project directory, from the terminal command-line:
> source ./venv/bin/activate
At this point, you might want to install packages into your virtual enviromment. Which packages will depend on the particulars of your project, but you can start with Dash:
> pip install dash
Every so often, you will want to catpure which packages have been installed into your virtual environment:
> pip freeze > requirements.txt
You will want to commit requirements.txt
to your git repository, as this contains the instructions for someone to reproduce your virtual environment.
To reproduce it, a colleague (perhaps you!) will have to create and activate a virtual environment, then:
> pip install -r requirements.txt
These are the very basics for how to set up and maintain a Python project. As you gain experience, you will likely adpat these ideas to your evolving needs.