3
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?

More than 3 years have passed since last update.

ubuntu18.04でpyenv,virtualenvをインストールする

Last updated at Posted at 2021-07-13

ubuntu18.04でpyenv,virtualenvをインストールする
ubuntu向け記事が少なかったので作成(Mac向け記事は多かった)

インストール

git clone https://github.com/pyenv/pyenv.git  ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

~/.bashrcに以下の内容を追記

# pyenv 
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

有効化

$ source ~/.bashrc

pyenvで利用するpythonバージョンのインストール

pyenv install x.x.x
ex) python 3.9.1 をインストールする場合
pyenv install 3.9.1

利用可能なバージョンはpyenv install --listで確認可能

pyenv install --list

仮想環境を作成する(ex. myenv)

pyenv virtualenv 3.9.1 myenv

仮想環境に入る

$ pyenv activate myenv

python3バージョン確認(3.9.1と表示されればOK)

python3 -V

pip3を使ってpython3のパッケージをインストールする

pip3 install numpy

仮想環境を抜ける

$ pyenv deactivate

仮想環境を削除する

$ pyenv uninstall myenv

これでOK

pythonのバージョンが切り替わらない

pyenv local 3.9.1 と指定してもpythonはホストPCのバージョンを使うという現象があった。
以下など色々触っているうちに解決した。原因分からないが備忘録として残しておく。

pythonのバージョンが切り替わらない

公式GitHubを参考に、
~/.bashrcに以下の内容を追記するなどした

# pyenv 
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

pyenv activate myenv時にFailed to activate virtualenv.とエラーが出る

エラー内容

Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.

以下を実行すると解決した

# enable pyenv (if necessary)
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

参考

pyenv + pyenv-virtualenv の使い方
pyenv と pyenv-virtualenv で環境構築
pyenv と pyenv-virtualenv をインストールする
pyenvとpyenv-virtualenvの自分流使い方
pyenvでpythonの仮想環境を構築
pyenv の activate って何? ( pyenv shell との違い )
Pythonにおける仮想環境「venv」について

3
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
3
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?