4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

virtualenv で仮想環境を立てる時のチートシート

Last updated at Posted at 2017-10-27

はじめに

virtualenvはクリーン環境をすぐに作れるのでちょっとPythonのライブラリ試して見たい時にで実行できてとっても便利です。でもでも、virtualenvはインストールしたけど使うのは久しぶりすぎて、いざ試す時にコマンド忘れてググる事が多々。。加えて、セットアップ系の記事はpyenvやanacondaなどと複合しているものが多いので自分用のメモ。

環境

ハード:MacBook Air
OS:macOS Sierra(10.12.6)
Python:3.6.1
virtualenv:15.1.0

チートシート

新規の仮想環境を立てる

カレントディレクトリに<仮想環境名>のディレクトリが生成されます。

コマンド
<ディレクトリ>$ virtualenv <仮想環境名>
実行結果
Using base prefix '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6'
New python executable in <ディレクトリ>/<仮想環境名>/bin/python3.6
Also creating executable in <ディレクトリ>/<仮想環境名>/bin/python
Installing setuptools, pip, wheel...done.

仮想環境を起動

コマンド
<ディレクトリ>$ cd <仮想環境ディレクトリ>
<仮想環境ディレクトリ>$ source bin/activate
実行結果
(<仮想環境名>) <仮想環境ディレクトリ> <ユーザー名>$ 

仮想環境を停止

コマンド
$ deactivate

おまけ

virtualenvインストール

ココまで書いてインストールがないのも違和感があるので念のため。

コマンド
pip3 install virtualenv

構築と起動を一発実行するシェルスクリプト

そもそも忘れてしまうのならば、大した量では無いですがシェルスクリプトにしました。
シェルスクリプト実行後も階層移動を保持したいためsoruceで実行しています。

activevirtualenv.sh
# !/bin/sh
virtualenv $1
cd $1/
source bin/activate
コマンド
$ source activevirtualenv.sh <仮想環境名>

おわりに

自分でチートシートって書くと覚えるし、理解も深まるので意外と良いかも。
virtualenv以外にも、jupyterなど開始、終了のstepがあるものは忘れっぽいので、今後もこまめにまとめていきたいと思います。

4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?