Install Python the right way

Installing Python is fairly easy. You can go to their official site and download the latest python version or browse through the list of versions and download it. This may be okay if you are planning to learn python, working with the same version and all. But what if you have multiple projects with multiple python projects? It may not be practical for you to download every python version as per the project. Therefore you require a python version manager for this task.

Pyenv is the simple solution for this. It easily lets you install any python version and switch between them with ease.

Getting Started

We will look into how to get pyenv up and running in ubuntu. You can always refer to the official docs for all other OS.

Ubuntu already comes with python preinstalled.

<span>$ </span>python <span>-V</span>
Python 2.7.17
<span>$ </span>which python
/usr/bin/python
<span>$ </span>python <span>-V</span>
Python 2.7.17

<span>$ </span>which python
/usr/bin/python
$ python -V Python 2.7.17 $ which python /usr/bin/python

Enter fullscreen mode Exit fullscreen mode

1. Before pyenv, python build dependencies must be installed https://github.com/pyenv/pyenv/wiki#suggested-build-environment

sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
sudo apt-get update; sudo apt-get install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Enter fullscreen mode Exit fullscreen mode

2. Clone pyenv to the root " $HOME/.pyenv"

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Enter fullscreen mode Exit fullscreen mode

3. Defining environment variable PYENV_ROOT to point to the pyenv repo and add $PYENV_ROOT/bin to your $PATH command line access

Zsh:

<span>echo</span> <span>'export PYENV_ROOT="$HOME/.pyenv"'</span> <span>>></span> ~/.zshrc
<span>echo</span> <span>'export PATH="$PYENV_ROOT/bin:$PATH"'</span> <span>>></span> ~/.zshrc
<span>echo</span> <span>'export PYENV_ROOT="$HOME/.pyenv"'</span> <span>>></span> ~/.zshrc
<span>echo</span> <span>'export PATH="$PYENV_ROOT/bin:$PATH"'</span> <span>>></span> ~/.zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc

Enter fullscreen mode Exit fullscreen mode

Bash:

<span>echo</span> <span>'export PYENV_ROOT="$HOME/.pyenv"'</span> <span>>></span> ~/.bash_profile
<span>echo</span> <span>'export PATH="$PYENV_ROOT/bin:$PATH"'</span> <span>>></span> ~/.bash_profile
<span>echo</span> <span>'export PYENV_ROOT="$HOME/.pyenv"'</span> <span>>></span> ~/.bash_profile
<span>echo</span> <span>'export PATH="$PYENV_ROOT/bin:$PATH"'</span> <span>>></span> ~/.bash_profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

Enter fullscreen mode Exit fullscreen mode

Ubuntu Desktop:

<span>echo</span> <span>'export PYENV_ROOT="$HOME/.pyenv"'</span> <span>>></span> ~/.bashrc
<span>echo</span> <span>'export PATH="$PYENV_ROOT/bin:$PATH"'</span> <span>>></span> ~/.bashrc
<span>echo</span> <span>'export PYENV_ROOT="$HOME/.pyenv"'</span> <span>>></span> ~/.bashrc
<span>echo</span> <span>'export PATH="$PYENV_ROOT/bin:$PATH"'</span> <span>>></span> ~/.bashrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc

Enter fullscreen mode Exit fullscreen mode

You can view the respective file to check if the change has occurred.
In Zsh, view ~/.zshrc file if the following code is there at the end

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH"

Enter fullscreen mode Exit fullscreen mode

4. Add pyenv init to the shell which enables shims and autocompletion

Zsh:

<span>echo</span> <span>-e</span> <span>'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi'</span> <span>>></span> ~/.zshrc
<span>echo</span> <span>-e</span> <span>'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi'</span> <span>>></span> ~/.zshrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc

Enter fullscreen mode Exit fullscreen mode

Bash:

<span>echo</span> <span>-e</span> <span>'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi'</span> <span>>></span> ~/.bash_profile
<span>echo</span> <span>-e</span> <span>'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi'</span> <span>>></span> ~/.bash_profile
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Enter fullscreen mode Exit fullscreen mode

Ubuntu Desktop:

<span>echo</span> <span>-e</span> <span>'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi'</span> <span>>></span> ~/.bashrc
<span>echo</span> <span>-e</span> <span>'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi'</span> <span>>></span> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc

Enter fullscreen mode Exit fullscreen mode

*Note please refer to https://github.com/pyenv/pyenv if any problem occurs

Similarly, after doing this, there will be the addition of a new line in ~/.zshrc file

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi
export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi

Enter fullscreen mode Exit fullscreen mode

5. Restart your shell/terminal for changes to occur

<span>exec</span> <span>"</span><span>$SHELL</span><span>"</span>
<span>exec</span> <span>"</span><span>$SHELL</span><span>"</span>
exec "$SHELL"

Enter fullscreen mode Exit fullscreen mode

All done now. You check if it is working as it must by running $ which python in terminal. You will notice that the path is changed.

<span>$ </span>which python
/home/user/.pyenv/shims/python
<span>$ </span>which python
/home/user/.pyenv/shims/python
$ which python /home/user/.pyenv/shims/python

Enter fullscreen mode Exit fullscreen mode

Also, you can check if pyenv is working by checking its version.

<span>$pyenv</span> <span>--version</span>
pyenv 1.2.22-63-g421ff608
<span>$pyenv</span> <span>--version</span>
pyenv 1.2.22-63-g421ff608
$pyenv --version pyenv 1.2.22-63-g421ff608

Enter fullscreen mode Exit fullscreen mode

List Python Versions

You can list available python version in your system by

pyenv versions
pyenv versions
pyenv versions

Enter fullscreen mode Exit fullscreen mode

For listing available python version to download by

pyenv <span>install</span> <span>--list</span>
pyenv <span>install</span> <span>--list</span>
pyenv install --list

Enter fullscreen mode Exit fullscreen mode

Install Python version

pyenv <span>install </span>3.9.1
pyenv <span>install </span>3.9.1
pyenv install 3.9.1

Enter fullscreen mode Exit fullscreen mode

Change global python version by

pyenv global 3.9.1
pyenv global system <span>#changes back to the system python</span>
pyenv global 3.9.1
pyenv global system <span>#changes back to the system python</span>
pyenv global 3.9.1 pyenv global system #changes back to the system python

Enter fullscreen mode Exit fullscreen mode

Change local python version (application-specific) by

pyenv <span>local </span>3.9.1
pyenv <span>local </span>3.9.1
pyenv local 3.9.1

Enter fullscreen mode Exit fullscreen mode

原文链接:Install Python the right way

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
Pain changes people. However, love will finally guide them back.
伤痛会改变一个人,但爱最终总会让你找回最初的自己
评论 抢沙发

请登录后发表评论

    暂无评论内容