Debian12(bookworm)でpipenvの仮想環境を作る。(pipenv、Nginx、uWSGIの連携)

Django

はじめに

pipenvを使ってプロジェクトディレクトリに仮想環境を作る方法です。
Debian12(bookworm)では、pipenv、Nginx、uWSGI、Djangoでソケット通信とHTTP通信の環境を作る方法の方法で仮想環境を作るとエラーになります。
そのため、システムにpipenvをインストールして、システムのPythonを元に仮想環境を作ります。

Debian12でsystemdからuwsgiを起動したときのエラー

Debian12(bookworm)でpipenvとpyenvで作成した仮想環境からはencodingsモジュールが参照できずにエラーになります。
システムのPythonを元に仮想環境を作ることで、エラーを回避します。
システムに標準でインストールされているPythonしか使えないのが欠点です。

[uWSGI] getting INI configuration from /home/www/wsgi//uwsgi.ini
*** Starting uWSGI 2.0.28 (64bit) on [Wed Dec 25 10:50:37 2024] ***
compiled with version: 12.2.0 on 25 December 2024 01:49:45
os: Linux-6.1.0-28-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.119-1 (2024-11-22)
nodename: gamma
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /
writing pidfile to /run/proj/uwsgi-proj.pid
detected binary path: /home/www/wsgi/proj/.venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
setgid() to 33
setuid() to 33
chdir() to /home/www/wsgi/proj
your processes number limit is 514581
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /run/proj/uwsgi-proj.sock fd 4
Python version: 3.13.1 (main, Dec 25 2024, 10:39:14) [GCC 12.2.0]
PEP 405 virtualenv detected: /home/www/wsgi/proj/.venv
Set PythonHome to /home/www/wsgi/proj/.venv
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007f090df4b7c0 (most recent call first): <no Python frame>
VACUUM: unix socket /run/proj/uwsgi-proj.sock removed.

環境

pipenvの仮想環境で使うPythonとDjangoは最新バージョンを使います。

 - Debian bookworm
 - Nginx 1.18.0
 - Python 3.13.1
 - Django 5.1.4
 - uwsgi 2.0.28

ディレクトリ構造は以下の通りです。
uWSGIの設定ファイル「uwsgi.ini」とpipenvの仮想環境ディレクトリ「.venv」はDjangoのプロジェクトを作成したディレクトリに置きます。

Nginx
  /etc/nginx/sites-available/proj.conf

PostgreSQL
  /etc/postgresql/13/main/postgresql.conf

Django
 /home/www/wsgi/proj
   ┣ -- proj
        ┣ -- urls.py
   ┣ -- app
   ┣ -- templates
   ┣ -- static
   ┣ -- db.sqlite3
   ┣ -- uwsgi.log
   ┣ -- uwsgi.ini
   ┣ -- .venv

必要なパッケージのインストール

pipenvでDjangoのプロジェクトを作成したディレクトリに仮想環境を作成するのに必要なパッケージをインストールします。

sudo apt install -y  gcc make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

sudo apt install libpcre3 libpcre3-dev python3-dev
sudo apt install uwsgi-plugin-python3
sudo apt install pipenv

仮想環境をの作成

Djangoのプロジェクト「proj」を/home/www/wsgi/に作成します。

mkdir /home/www/wsgi/proj

プロジェクト直下に仮想環境を作成してpipenvをインストールします。
「which pipenv」を実行して、パスが現在の仮想環境に存在するか確認します。
システムのPythonは「3.11.2」です。

cd /home/www/wsgi/proj
pipenv --python 3.11.2
pipenv shell
pip install --upgrade pip
which pip
    /home/www/wsgi/proj/.venv/bin/pip
pip install pipenv
which pipenv
     /home/www/wsgi/proj/.venv/bin/pipenv

pipenv install django
pipenv install uwsgi

設定ファイルとアプリを作成します。

cd /home/www/wsgi/proj
django-admin startproject config .
django-admin startapp app

別の環境からPipfileとPipfile.lockをコピーして、パッケージをインストールします。

pipenv sync
pipenv update

Comments