Build a Python virtual environment using venv (Django + MySQL ①)

About this article

This article corresponds to ** Part.1 ** of the articles related to "Learn how to use Docker through building an environment for Django + MySQL".

  1. ** Build a Python virtual environment using venv (this article) **
  2. Consider the description of Dockerfile
  3. Consider the description of docker-compose.yml
  4. Edit the configuration file and execute docker-compose up
  5. Adjust container startup timing between dependent services

Introduction

In this article, we will first use ** venv to build a Python3 virtual environment **.

Create a virtual environment for a specific project with venv based on any version of Python3 prepared on the host PC with Pyenv. The purpose is ** to be able to manage the version of Python on a project-by-project basis ** and to make the installation of packages by ** pip more efficient / lightweight **.

This article uses ** Python 3.7.7 **.

Prepare python

# Python 3.7.7 installation
$ pyenv install 3.7.7

#3 version of Python3 to use.7.To 7
$ pyenv global 3.7.7

#Version confirmation
$ python3 --version
Python 3.7.7

Creating a virtual environment

** Create a directory for your project and then create a virtual environment in it **.

#Creating a project directory
$ mkdir django_starter

#Move directory
$ cd django_starter

#Creating a virtual environment
$ python3 -m venv .venv

The .venv at the end of the command is the pathname. Here, it is set to .venv according to the convention, but of course it is okay to put a directory between them to deepen the hierarchy or give it a name of your choice.

Then ** enable the virtual environment and confirm the activation **.

#activation
$ source .venv/bin/activate

#Check "Where are you using Python?"
(.venv) $ which python
(Abbreviation)/django_starter/.venv/bin/python

If the virtual environment name you gave earlier is displayed before the shell user name, you can confirm that it is enabled. I think that ** Python3` for this project is placed in the path of the virtual environment placed in the project, and I will use it **. By the way,

$ deactivate

You can get out of the virtual environment just by executing.

Package installation

Use pip in the virtual environment to install the required packages. Install some of the ones that are confirmed to be used for the purpose of this article group in advance.

#Installation
(.venv) $ pip install django==3.0.8 mysqlclient==2.0.1 pathlib==1.0.1

#Check installed packages
(.venv) $ pip freeze

Next, create requirements.txt based on these.

(.venv) $ pip freeze >> requirements.txt

The contents should look like this.

requirements.txt


asgiref==3.2.10
Django==3.0.8
mysqlclient==2.0.1
pathlib==1.0.1
pytz==2020.1
sqlparse==0.3.1

In the future, when sharing the environment or copying the environment into the container, installation will be performed based on the description in this file. ($ pip install -r requirements.txt)

Create a Django project

From the next time, I will set up Docker related files, but before that, I will create a Django project.

(.venv) $ pwd
(Abbreviation)/django_starter

(.venv) $ django-admin startproject config .

Of course, the name of the directory generated by the command can be anything, but if it is the same name as the parent directory, it will be confusing, so leave it as config. . Is the path specification of the creation location.

Current directory structure

django_starter
    ├── config
    │     ├── __init__.py
    │     ├── asgi.py
    │     ├── settings.py
    │     ├── urls.py
    │     └── wsgi.py
    ├── manage.py
    ├── .venv
    │   └── (Abbreviation)
    └── requirements.txt

At the end

By using a virtual environment, you can ** avoid installing packages that are not related to the project and manage only what you need **. Similarly, if you have something that you only use for this project, ** you don't have to pollute your local main environment (?) **, which is very nice.

We will use this to build an environment inside the container that will be put into practice.

In the next article, we will consider ** the description of the Dockerfile **. Click here for more ↓ "2. Consider the description of Dockerfile"

Thank you for visiting.

Recommended Posts

Build a Python virtual environment using venv (Django + MySQL ①)
Build Python + django + nginx + MySQL environment using docekr
Build a python virtual environment with pyenv
Try using virtualenv, which can build a virtual environment for Python
How to create a Python virtual environment (venv)
Build a Django environment on Raspberry Pi (MySQL)
[Python] Build a Django development environment with Docker
Build a simple Python virtual environment without pyenv
Build a virtual environment with pyenv and venv
[Venv] Create a python virtual environment on Ubuntu
Quickly build a Python Django environment with IntelliJ
Building a Python virtual environment
venv: Python virtual environment management
Build a Python environment offline
python standard virtual environment venv
Building a Python virtual environment
A memo to create a virtual environment (venv) before Django
Build a Django development environment using pyenv-virtualenv on Mac
Python: Creating a virtual environment (venv), starting and stopping
Build a python virtual environment with virtualenv and virtualenvwrapper
How to build a Django (python) environment on docker
Build a Python environment on your Mac using pyenv
Building and enabling a python virtual environment, etc. (venv)
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Python development environment using pyenv on MacOS
Build a Django environment for Win10 (with virtual space)
Build python virtual environment with virtualenv
Build a go environment using Docker
Build Mysql + Python environment with docker
Create a virtual environment with Python!
Building a virtual environment with Python 3
Build a python3 environment on CentOS7
How to build a new python virtual environment on Ubuntu
[Django3] Display a web page in Django3 + WSL + Python virtual environment
Install Django in a pipenv virtual environment
Python development environment for macOS using venv 2016
Using venv in Windows + Docker environment [Python]
Build a local development environment for Lambda + Python using Serverless Framework
Building a virtual environment using homebrew + pyenv-virtualenv
[Python] Create a virtual environment with Anaconda
[Python] Create a Batch environment using AWS-CDK
I want to build a Python environment
[Mac] Building a virtual environment for Python
Creating a python virtual environment on Windows
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build a Python + OpenCV environment on Cloud9
Build a Python execution environment using GPU with GCP Compute engine
Build a modern Python environment with Neovim
Add a Python virtual environment to VSCode
Prepare a Python virtual environment for your project with venv with VS Code
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
[Mac] Build a Python 3.x environment at the fastest speed using Docker
Build a Python virtual environment that anyone can understand September 2016 (pyenv + virutalenv)
Ubuntu18.04.05 Creating a python virtual environment in LTS
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
Simply build a Python 3 execution environment on Windows
Build a python environment with ansible on centos6
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
Create a python3 build environment with Sublime Text3
Build a Django environment with Vagrant in 5 minutes