LoginSignup
0
0

Python 開発環境のセットアップ 備忘録

Last updated at Posted at 2023-04-29

Python 開発環境のセットアップ 備忘録

Python バージョン切り替え

py -3.11 script.py
  • py options
$ py --help
usage:
py [launcher-args] [python-args] [script [script-args]]

Launcher arguments:
-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version

Python 仮想環境作成

cd project_dir
py -m venv venv
  • python options
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
-c cmd : program passed in as string (terminates option list)
-m mod : run library module as a script (terminates option list)
  • venv options
python -m venv /path/to/new/virtual/environment

Python 仮想環境 Activate

  • Windows
.\venv\Scripts\activate

次のバッチファイルを作成しておくと、コマンドプロンプト起動時に仮想環境をアクティベートできます。

activate.bat
start .\venv\Scripts\activate

次のバッチファイルは仮想環境をアクティベートして、Python スクリプトを実行する方法です。

main.bat
call .\venv\Scripts\activate
py main.py
  • Linux
. ./venv/Scripts/activate

Python 仮想環境 Deactivate

deactivate

Python パッケージ管理

pip freezeを実行して、インストールされたパッケージのリストを、requirements.txtとして保存します。

(venv) pip freeze > requirements.txt

pip installコマンドの-rオプションで、requirements.txtに定義されたパッケージを順番にインストールします。

(venv) pip install -r requirements.txt

Python 開発環境 for Visual Studio Code

Visual Studio Code で Python 開発環境を構築する手順は、こちらの記事を参照してください。

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