2
2

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をVSCodeで実行する

Posted at

はじめに

VSCodeでPythonの仮想環境venvに入ったと思ってpip install したら、

グローバルにインストールするのを2回やらかしたので記事にします。

この記事でわかること

  • VSCodeのターミナルでPythonの仮想環境venvの使い方が分かる

この記事の対象者

  • Python使う人
  • VSCode使う人

動作環境

  • Windows11
  • Python 3.11.9 (pyenv-win)
  • VSCode 1.97.2
    普通のPythonやWinPythonでも一緒です。

やり方

  1. Git使っている場合は.gitignoreに.venv/を追加しておく
  2. VSCodeのターミナルで以下を実行(今回はbash)
    python -m venv .venv
  3. VSCodeの右下になんか出るのでOK。またはPythonファイルにフォーカスして、右下のPython部分をクリックしてインタープリターを選択
    image.png
  4. Pythonファイルにフォーカスして、右下を確認
    image.png
  5. VSCodeのターミナルで以下を実行(最初の.はsorce、コマンドプロンプトなら.venv/Scripts/activate.bat)
    . .venv/Scripts/acitvate
  6. pip listをしてみる
    $ pip list
    Package     Version
    ----------- -------
    pip         25.0.1
    setuptools  65.5.0
    (.venv) 
    
  7. (.venv)が最後につくようになる。この状態が仮想環境なのでpip installすると仮想環境内に保存されるようになる

抜ける時

  1. 抜ける時はターミナルでDeactivateを実行
  2. インタープリターの変更

まとめ

もうグローバルインストールしません。(自戒)
あとVSCode右上の再生ボタンでも大丈夫という噂があります。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?