0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python 仮想環境 venv

Last updated at Posted at 2025-04-14

はじめに

python3でパッケージが無いと怒られる。
嘗てはpipでインストールしていたけど、venvを使うのが推奨されている。

$ python3
Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
>>> quit()

仮想環境の構築

python3 -m venv hoge
というコマンドで仮想環境を構築しようとし、venvがインストールされてなくて失敗する場合、エラーメッセージの指示に従いvenvをインストールする。
sudo apt install python3.12-venv

改めて仮想環境を構築(少し時間かかる)
python3 -m venv hoge

仮想環境でpipを使う

$ source hoge/bin/activate
(hoge) $ pip install numpy

のように仮想環境に入り、パッケージをインストールする。

(hoge) $ python3
Python 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.arange(3)
array([0, 1, 2])
>>> quit()
(hoge) $ deactivate
$

python3でパッケージが使用できることを確認する。
deactivateコマンドで仮想環境を抜ける。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?