python:env_ubuntu
目次
Python関連|Python開発環境を作る(Ubuntu)
OS(Ubuntu)にはじめから組み込まれているPython環境をそのまま使うのはあまりよろしくない。
自分自身のアカウント専用のPython環境(仮想環境という)を作るのがよさげ。
参考
実行環境
ノートパソコン(Sony VPCF24)にインストールした Ubuntu22.04
手順1 Python仮想環境の導入
参考サイトの写経です。
現環境の確認
hiroshi@VPCF24AJ-ubuntu:~$ which python3 /usr/bin/python3 hiroshi@VPCF24AJ-ubuntu:~$ cd /usr/bin/ hiroshi@VPCF24AJ-ubuntu:/usr/bin$ ls -l | grep python lrwxrwxrwx 1 root root 24 6月 29 21:14 pdb3.10 -> ../lib/python3.10/pdb.py lrwxrwxrwx 1 root root 31 3月 25 21:41 py3versions -> ../share/python3/py3versions.py lrwxrwxrwx 1 root root 10 3月 25 21:41 python3 -> python3.10 lrwxrwxrwx 1 root root 17 3月 25 21:41 python3-config -> python3.10-config -rwxr-xr-x 1 root root 960 12月 23 2020 python3-futurize -rwxr-xr-x 1 root root 964 12月 23 2020 python3-pasteurize -rwxr-xr-x 1 root root 5905480 6月 29 21:14 python3.10 lrwxrwxrwx 1 root root 34 6月 29 21:14 python3.10-config -> x86_64-linux-gnu-python3.10-config lrwxrwxrwx 1 root root 34 3月 25 21:41 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.10-config -rwxr-xr-x 1 root root 3123 6月 29 21:14 x86_64-linux-gnu-python3.10-config hiroshi@VPCF24AJ-ubuntu:/usr/bin$
pip、pipenvのインストール
pip は python独自のパッケージ管理ツール
pipenv は Python仮想環境を管理するしくみ
hiroshi@VPCF24AJ-ubuntu:/usr/bin$ sudo apt update hiroshi@VPCF24AJ-ubuntu:/usr/bin$ sudo apt install python3-pip : hiroshi@VPCF24AJ-ubuntu:/usr/bin$ pip --version pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10) hiroshi@VPCF24AJ-ubuntu:/usr/bin$ hiroshi@VPCF24AJ-ubuntu:/usr/bin$ pip install pipenv Defaulting to user installation because normal site-packages is not writeable Collecting pipenv Downloading pipenv-2022.7.4-py2.py3-none-any.whl (3.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.7/3.7 MB 5.1 MB/s eta 0:00:00 Requirement already satisfied: certifi in /usr/lib/python3/dist-packages (from pipenv) (2020.6.20) Collecting virtualenv Downloading virtualenv-20.15.1-py2.py3-none-any.whl (10.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.1/10.1 MB 5.7 MB/s eta 0:00:00 Collecting pip>=22.0.4 Downloading pip-22.1.2-py3-none-any.whl (2.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 5.3 MB/s eta 0:00:00 Collecting virtualenv-clone>=0.2.5 Downloading virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB) Requirement already satisfied: setuptools>=36.2.1 in /usr/lib/python3/dist-packages (from pipenv) (59.6.0) Collecting platformdirs<3,>=2 Downloading platformdirs-2.5.2-py3-none-any.whl (14 kB) Collecting distlib<1,>=0.3.1 Downloading distlib-0.3.5-py2.py3-none-any.whl (466 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 467.0/467.0 KB 3.3 MB/s eta 0:00:00 Requirement already satisfied: six<2,>=1.9.0 in /usr/lib/python3/dist-packages (from virtualenv->pipenv) (1.16.0) Collecting filelock<4,>=3.2 Downloading filelock-3.7.1-py3-none-any.whl (10 kB) Installing collected packages: distlib, virtualenv-clone, platformdirs, pip, filelock, virtualenv, pipenv WARNING: The script virtualenv-clone is installed in '/home/hiroshi/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts pip, pip3 and pip3.10 are installed in '/home/hiroshi/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The script virtualenv is installed in '/home/hiroshi/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts pipenv and pipenv-resolver are installed in '/home/hiroshi/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed distlib-0.3.5 filelock-3.7.1 pip-22.1.2 pipenv-2022.7.4 platformdirs-2.5.2 virtualenv-20.15.1 virtualenv-clone-0.5.7 hiroshi@VPCF24AJ-ubuntu:/usr/bin$
仮想環境のインストール
pipenvを使ってPython仮想環境を構築する
hiroshi@VPCF24AJ-ubuntu:~$ mkdir pipenv1 hiroshi@VPCF24AJ-ubuntu:~$ cd pipenv1 hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ python3 -m pipenv --python python3 install /usr/lib/python3/dist-packages/pkg_resources/__init__.py:116: PkgResourcesDeprecationWarning: 0.1.43ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( /usr/lib/python3/dist-packages/pkg_resources/__init__.py:116: PkgResourcesDeprecationWarning: 1.1build1 is an invalid version and will not be supported in a future release warnings.warn( Creating a virtualenv for this project... Pipfile: /home/hiroshi/pipenv1/Pipfile Using /usr/bin/python3 (3.10.4) to create virtualenv... ⠇ Creating virtual environment...created virtual environment CPython3.10.4.final.0-64 in 510ms creator CPython3Posix(dest=/home/hiroshi/.local/share/virtualenvs/pipenv1-RRIdUoVq, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/hiroshi/.local/share/virtualenv) added seed packages: pip==22.1.2, setuptools==62.6.0, wheel==0.37.1 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator ✔ Successfully created virtual environment! Virtualenv location: /home/hiroshi/.local/share/virtualenvs/pipenv1-RRIdUoVq Creating a Pipfile for this project... Pipfile.lock not found, creating... Locking [dev-packages] dependencies... Locking [packages] dependencies... Updated Pipfile.lock (e4eef2)! Installing dependencies from Pipfile.lock (e4eef2)... 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00 To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ ls -l 合計 8 -rw-rw-r-- 1 hiroshi hiroshi 139 7月 19 09:59 Pipfile -rw-r--r-- 1 hiroshi hiroshi 454 7月 19 09:59 Pipfile.lock hiroshi@VPCF24AJ-ubuntu:~/pipenv1$
あひる?がちょう?がカワイイ。
仮想環境へ切り替え
hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ python3 -m pipenv shell /usr/lib/python3/dist-packages/pkg_resources/__init__.py:116: PkgResourcesDeprecationWarning: 0.1.43ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( /usr/lib/python3/dist-packages/pkg_resources/__init__.py:116: PkgResourcesDeprecationWarning: 1.1build1 is an invalid version and will not be supported in a future release warnings.warn( Launching subshell in virtual environment... hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ . /home/hiroshi/.local/share/virtualenvs/pipenv1-RRIdUoVq/bin/activate (pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ (pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$
何やらWarningが出ているが、、、
プロンプトの前方に(pipenv1)と出たので、とりあえずは Python仮想環境に切り替わったようだ。
(pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ which python /home/hiroshi/.local/share/virtualenvs/pipenv1-RRIdUoVq/bin/python (pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ which pip /home/hiroshi/.local/share/virtualenvs/pipenv1-RRIdUoVq/bin/pip (pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ pip freeze (pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$
システム(Ubuntu)のPython環境からは、ちゃんと分離されているっぽい。
この環境内でpipインストールしたモジュールやライブライは、システムや他の仮想環境のPython環境に一切影響を与えない。
仮想環境から抜ける
(pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ (pipenv1) hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ exit exit hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ which python hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ which python3 /usr/bin/python3 hiroshi@VPCF24AJ-ubuntu:~/pipenv1$
まとめ
仮想環境を作る
mkdir -p <仮想環境へのパス> cd <仮想環境へのパス> python3 -m pipenv --python python3 install
仮想環境に入る
cd <仮想環境へのパス> python3 -m pipenv shell
仮想環境から抜ける
exit
手順2 VS Codeの導入
IDE(統合開発環境)として Visual Studio Codeをインストールする。
VS Codeインストール
hiroshi@VPCF24AJ-ubuntu:~/pipenv1$ code コマンド 'code' が見つかりません。次の方法でインストールできます: sudo snap install code hiroshi@VPCF24AJ-ubuntu:~/pipenv1$
って言われる。
素直に従ってもよいのだろうが、特に理由もなくGUIアプリの「Ubuntu Software」からインストールした。(Snapストア)
VS Code起動、日本語化
Python拡張機能の導入
手順3 VS CodeをPython IDEとして使う
python/env_ubuntu.txt · 最終更新: 2022/07/19 11:03 by nipa28