1
1

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.

venvで立ち上げた仮想環境の中で指定したバージョンのPythonを起動させる方法

Posted at

#直面した事象

ローカルのTerminalで__pyenv localで設定したPythonのバージョンが、python -m venv__で立ち上げた仮想環境の中で引き継がれない。

#やりたいこと

  • venv仮想環境野中で、Python 3.7.9系を使いたい。(Python 3.9.6ではなく)
  • Pyenv localで3.7.9に変えたのに、そのディレクトリでvenvコマンドを叩いて仮想環境を作ると、仮想環境野中では3.7.9が立ち上がらない。

##成功した解決策

(参考)

解決方法

https://github.com/pyenv/pyenv#homebrew-on-mac-os-x を読み直してみたら、「pyenv initを使うように!」とあったので、早速実行。

$ pyenv init
# Load pyenv automatically by appending
# the following to ~/.bash_profile:

eval "$(pyenv init -)"
```

指示通り、~/.bash_profileにeval "$(pyenv init -)"を追記して、source ~/.bash_profileを実行したところ…

```bash:
$ python -V
Python 3.5.6
```

解決!
ドキュメントはきちんと読めということですね(反省)

####書いてあるとおり、__bash_profile__に1行追記したところ解決しましたsunny:

Terminal
electron@diynoMacBook-Pro ~ % ls ~/.bash_profile
/Users/electron/.bash_profile
electron@diynoMacBook-Pro ~ % cat ~/.bash_profile
eval OPAM_SWITCH_PREFIX='/Users/electron/.opam/default'; export OPAM_SWITCH_PREFIX; CAML_LD_LIBRARY_PATH='/Users/electron/.opam/default/lib/stublibs:Updated by package ocaml'; export CAML_LD_LIBRARY_PATH; OCAML_TOPLEVEL_PATH='/Users/electron/.opam/default/lib/toplevel'; export OCAML_TOPLEVEL_PATH; PATH='/Users/electron/.opam/default/bin:/Users/electron/.pyenv/shims:/Users/electron/.pyenv/bin:/Users/electron/.nodebrew/current/bin:/Users/electron/.nodebrew/current/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/electron/.cargo/bin'; export PATH;
export PATH=~/bin:$PATH
electron@diynoMacBook-Pro ~ % 
  • Vimで編集
Terminal
electron@diynoMacBook-Pro ~ % vi ~/.bash_profile
  • 最終行に、__eval "$(pyenv init -)"__を追記した。
Terminal
electron@diynoMacBook-Pro ~ % cat ~/.bash_profile
eval OPAM_SWITCH_PREFIX='/Users/electron/.opam/default'; export OPAM_SWITCH_PREFIX; CAML_LD_LIBRARY_PATH='/Users/electron/.opam/default/lib/stublibs:Updated by package ocaml'; export CAML_LD_LIBRARY_PATH; OCAML_TOPLEVEL_PATH='/Users/electron/.opam/default/lib/toplevel'; export OCAML_TOPLEVEL_PATH; PATH='/Users/electron/.opam/default/bin:/Users/electron/.pyenv/shims:/Users/electron/.pyenv/bin:/Users/electron/.nodebrew/current/bin:/Users/electron/.nodebrew/current/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/electron/.cargo/bin'; export PATH;
export PATH=~/bin:$PATH
eval "$(pyenv init -)"
electron@diynoMacBook-Pro ~ %
  • __sourceで、bash_profile__を再読み込み。
Terminal
electron@diynoMacBook-Pro ~ % source ~/.bash_profile

###Pythonが3.7.9系に切り替わった

Terminal
electron@diynoMacBook-Pro ~ % python -V           
Python 3.7.9
electron@diynoMacBook-Pro ~ % python3 -V          
Python 3.7.9
electron@diynoMacBook-Pro ~ % 

###vemv仮想環境の中でも、Pythonが3.7.9系に切り替わっている

Terminal
electron@diynoMacBook-Pro ~ % python3 -m venv venv-test
electron@diynoMacBook-Pro ~ % source venv-test/bin/activate
(venv-test) electron@diynoMacBook-Pro ~ % python -V
Python 3.7.9
(venv-test) electron@diynoMacBook-Pro ~ % python3 -V
Python 3.7.9
(venv-test) electron@diynoMacBook-Pro ~ % 

##失敗した方法

この固定されたPythonバージョンを変更するにはどうすればよいかというと、結論としては、仮想環境内のPythonバージョンを切り替えるためには、venvの--オプションを利用して仮想環境を一度クリアする必要があります。

実際にやってみます。ローカル環境でpython -m venv [venvディレクトリ] --を実行し、作成済みの仮想環境をクリアします。

Terminal
electron@diynoMacBook-Pro ~ % python3 -m venv test
electron@diynoMacBook-Pro ~ % source test/bin/activate
(test) electron@diynoMacBook-Pro ~ % python --version
Python 3.9.6
(test) electron@diynoMacBook-Pro ~ % pip --version
pip 21.1.3 from /Users/electron/test/lib/python3.9/site-packages/pip (python 3.9)
(test) electron@diynoMacBook-Pro ~ % 
Terminal
(test) electron@diynoMacBook-Pro ~ % deactivate                  
electron@diynoMacBook-Pro ~ % python -m venv test --
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named venv
electron@diynoMacBook-Pro ~ % 
electron@diynoMacBook-Pro ~ % pyenv versions
  system
  2.7.16
  3.6.0
  3.6.1
  3.6.3
  3.6.3/envs/gpt2_ja
  3.6.6
  3.7.0
  3.7.4
* 3.9.0 (set by /Users/electron/.python-version)
  gpt2_ja
electron@diynoMacBook-Pro ~ % pyenv local 3.7.9
pyenv: version `3.7.9' not installed
electron@diynoMacBook-Pro ~ % pyenv install 3.7.9
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.9.tar.xz...
-> https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tar.xz
Installing Python-3.7.9...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

Installed Python-3.7.9 to /Users/electron/.pyenv/versions/3.7.9

electron@diynoMacBook-Pro ~ % 
electron@diynoMacBook-Pro ~ % pyenv versions
 electron@diynoMacBook-Pro ~ % pyenv local 3.7.9
electron@diynoMacBook-Pro ~ % pyenv versions 
  system
  2.7.16
  3.6.0
  3.6.1
  3.6.3
  3.6.3/envs/gpt2_ja
  3.6.6
  3.7.0
  3.7.4
* 3.7.9 (set by /Users/electron/.python-version)
  3.9.0
  gpt2_ja
electron@diynoMacBook-Pro ~ %
Terminal
electron@diynoMacBook-Pro ~ % python3 -m venv test       
electron@diynoMacBook-Pro ~ % source test/bin/activate   
(test) electron@diynoMacBook-Pro ~ % python --version
Python 3.9.6
(test) electron@diynoMacBook-Pro ~ % deactivate
Terminal
electron@diynoMacBook-Pro ~ % pyenv global 3.7.9
electron@diynoMacBook-Pro ~ % python3 -m venv test    
electron@diynoMacBook-Pro ~ % source test/bin/activate
(test) electron@diynoMacBook-Pro ~ % python --version 
Python 3.9.6
(test) electron@diynoMacBook-Pro ~ % python -V
Python 3.9.6
(test) electron@diynoMacBook-Pro ~ % 
(test) electron@diynoMacBook-Pro ~ % pyenv versions
  system
  2.7.16
  3.6.0
  3.6.1
  3.6.3
  3.6.3/envs/gpt2_ja
  3.6.6
  3.7.0
  3.7.4
* 3.7.9 (set by /Users/electron/.python-version)
  3.9.0
  gpt2_ja
(test) electron@diynoMacBook-Pro ~ % 
(test) electron@diynoMacBook-Pro ~ % python3 --version
Python 3.9.6
(test) electron@diynoMacBook-Pro ~ % python --version
Python 2.7.16
(test) electron@diynoMacBook-Pro ~ % 
(test) electron@diynoMacBook-Pro ~ % pyenv local 3.7.9
(test) electron@diynoMacBook-Pro ~ % python -V
Python 2.7.16
(test) electron@diynoMacBook-Pro ~ % pyenv shell 3.7.9
(test) electron@diynoMacBook-Pro ~ % python -V        
Python 2.7.16
(test) electron@diynoMacBook-Pro ~ % deactivate
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?