Lesson 7 - Virtual Environment and gitignore
7.1. Virtual Environment
- Virtual environments are a way to isolate Python code from each other. It allows you to run Python code in different environments, and use the same Python code in multiple environments.
- It gives you a separate Python environment for each project. It allows you to use different versions of Python for different projects.
- It is also useful for testing different versions of Python.
- Virtual environments are also a good way for using environmental variables that contain passwords or other sensitive information.
7.1.1 Virtual Environment Generated by Python
7.1.1.1 Create a virtual environment
1
python3 -m venv venv
7.1.1.2. Activate the virtual environment
1
source venv/bin/activate
7.1.1.3. Deactivate the virtual environment
1
deactivate
7.1.1.4. Remove the virtual environment
- To remove the virtual environment, you can use the
rmcommand. You are practically deleting the virtual environment folder.
1
rm -rf venv
7.1.2. Virtual Environment Generated by Anaconda
- Anaconda Docs: Creation of virtual environment
- it is a very convenient thing to use Anaconda to create a virtual environment. It creates a virtual environment for you outside of the project folder. And you can choose the version of Python you want to use.
7.1.2.1. Create a virtual environment
1
conda create -n venv python=3.8
7.1.2.2. Activate the virtual environment
1
conda activate venv
7.1.2.3. Deactivate the virtual environment
1
conda deactivate
7.3.2.4. Remove the virtual environment
1
conda remove --name venv --all
7.3.2.5. List the virtual environment
1
conda env list
or
1
conda info --envs
7.1.3. Working with .env File
Using .env Files for Environment Variables in Python Applications
- To create a
.envfile, you can use any text editor. - You can use the
.envfile to store environmental variables. .envfile should be stored in the project folder.
7.1.3.1. Create a .env file
1
touch .env
7.1.3.2. Syntax of .env file
1
2
3
4
5
6
7
8
# .env file
# VARIABLE_NAME="VALUE"
API_KEY=your_api_key
API_SECRET=your_api_secret
# Other variables
EMAIL_ADDRESS=your_email_address
EMAIL_PASSWORD=your_email_password
7.1.3.3. Import the .env file
- In order to import the
.envfile in Python, you can use thedotenvmodule.
dotenv can be installed as follows:
1
pip install python-dotenv
- in Python, you can use the
load_dotenvfunction to load the.envfile.
1
2
from dotenv import load_dotenv
load_dotenv()
7.1.3.4. Use the .env file in Python
- in order to read the variables from the
.envfile, you can use theosmodule. - in Python, you can use the
os.getenvfunction to read the variables from the.envfile.
1
2
3
import os
API_KEY = os.getenv("API_KEY")
API_SECRET = os.getenv("API_SECRET")
- if there is a possibility that the environmental variable isn’t set, you can set the default when calling the
os.getenvfunction.
1
2
3
import os
API_KEY = os.getenv("API_KEY", "your_api_key") # your_api_key is the default if the environmental variable is not set
API_SECRET = os.getenv("API_SECRET", "your_api_secret") # your_api_secret is the default if the environmental variable is not set
7.2. .gitignore File
- gitignore
- In order to ignore files in the project folder, you can use the
.gitignorefile. The.gitignorefile is a text file that contains a list of files and directories that you want to ignore when you commit your project to GitHub and in that way it protects the details you want to keep hidden from the public. - Beware, the files in the
.gitignorefile are ignored by Git. Thier changes are not tracked, and if deleted, you cannot revert them. - To create a
.gitignorefile, you can use any text editor and save it in the project folder.
1
touch .gitignore
7.2.1. Syntax of .gitignore file
- The
.gitignorefile is a list of files and directories to be ignored. Every new item is added to the next line.
1
2
3
4
5
6
7
# .gitignore file
# Ignore all text files
*.txt
# Ignore a directory and its contents
venv/
# Ignore a specific file
.env
7.2.2. Tools to automatically generate .gitignore file
- When making a new repository, GitHub gives an option to automatically generate a
.gitignorefile according to the selected template. You can selectPythonfrom the dropdown list.
- When making a new repository, GitHub gives an option to automatically generate a
- Gitignore.io generates a
.gitignorefile according to the selected template. You can selectPythonorVanilla Pythonfrom the dropdown list or type it into the text box. - You can then copy the generated contents from the web page and paste it into a new
.gitignorefile of your project.
- Gitignore.io generates a
This post is licensed under CC BY 4.0 by the author.
