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
rm
command. 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
.env
file, you can use any text editor. - You can use the
.env
file to store environmental variables. .env
file 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
.env
file in Python, you can use thedotenv
module.
dotenv
can be installed as follows:
1
pip install python-dotenv
- in Python, you can use the
load_dotenv
function to load the.env
file.
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
.env
file, you can use theos
module. - in Python, you can use the
os.getenv
function to read the variables from the.env
file.
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.getenv
function.
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
.gitignore
file. The.gitignore
file 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
.gitignore
file are ignored by Git. Thier changes are not tracked, and if deleted, you cannot revert them. - To create a
.gitignore
file, you can use any text editor and save it in the project folder.
1
touch .gitignore
7.2.1. Syntax of .gitignore
file
- The
.gitignore
file 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
.gitignore
file according to the selected template. You can selectPython
from the dropdown list.
- When making a new repository, GitHub gives an option to automatically generate a
- Gitignore.io generates a
.gitignore
file according to the selected template. You can selectPython
orVanilla Python
from 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
.gitignore
file of your project.
- Gitignore.io generates a
This post is licensed under CC BY 4.0 by the author.