Python Development Environment : pyenv & VS Code
While working on Python projects, we need to support projects with multiple versions of Python. Managing multiple versions in one machine is adifficult task. But if you use pyenv it becomes easy to manage.
What is pyenv?
pyenv is a tool to manage multiple Python versions. You may not need to install System Python if you use pyenv.
Installing pyenv [Windows]
Follow instructions as mentioned in this link . If you are using WINDOWS operating system the I would prefer Chocolatey package manager.
Chocolatey Installation
Chocolatey Installation
Use powershell with admin rights
> choco install pyenv-win
To test if the installation is successful
> pyenv
To see available python installation
> pyenv install -l
If you do not see you python version then,
> pyenv update
Installing pyenv [WSL2 - LINUX/Ubuntu]
Install Home Brew package manager. follow the the instructions HomeBrew and HomeBrew Installations
> /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
> test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"
> test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
> test -r ~/.profile && echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.profile
> echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.profile
> brew update
> sudo apt-get update
> sudo apt-get install build-essential procps curl file git
> brew install pyenv
Update (pyenv init --path) in .profile or .bashrc file
> nano ~/.profile
eval "$(pyenv init --path)"
> source ~/.profile
------------------------ OR -------------------------
> nano ~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init --path)"
fi
> source ~/.bashrc
Verifying pyenv installation
> pyenv --version
> pyenv update
Searching for specific installation
> pyenv install --list | select-string "3.10.2" [Windows powershell]
or
> pyenv install --list | grep '3.10.2'
Installing python
> pyenv install 3.10.2
> pyenv rehash
To Know which python in a particular directory/path
> pyenv which python
Checking Installed version
> pyenv version
> pyenv versions
Entering into a particular shell
> pyenv shell 3.10.2
Setting Global python version
> pyenv global 3.10.2
Setting local python version specific to a project or folder
Create a folder and CD to that folder
> pyenv local 3.10.2
This should create a file .python-version inside you folder
How to know which python version is in use -
Creating Virtual Environments for the projects
CD to you project folder
> python -V
Note: Incase python is not working. Check below settings on windows.
Go to "manage App Execution aliases" and switch off alias for both "python" and "python3"
Creating virtual environment
> python -m venv .venv
It will create a folder under project folder ".venv". All the activation scripts and pip installation will happen under this folder.
Check PIP before you activate the environment
> get-command pip
Now activate the environment
> .\.venv\Scripts\activate
> source .venv/bin/activate [for WS2/Ubuntu]
Now again check PIP -- Use powershell terminal
> get-command pip
Updating PIP in this virtual environment
> python -m pip install --upgrade pip wheel setuptools
Configuring Visual Studio Code
Cd to the project folder.
> code .
This will open Visual Studio Code
You can also open VS Code directly.
Install Python extension for VS Code
To open Command Pallet > Ctrl + Shift + P
do type "settings json" then select "Open workspace settings json"
update json -
"python.terminal.activateEnvironment": true
This will auto activate the python virtual environment when VS code started.
Package management
> pip freeze > requirements.txt
This file contains all the packages required for the project.
> pip install -r requirements.txt
Settings to set Formatter, Linter and debug environment follow this link
Use proper .gitignore file
Comments
Post a Comment