Python development environment setup.

Updated on 2024/11/05. Use venv instead of external tool virtualenvwrapper.

1. Install pyenv

Recent version of macOS does not come with python command. macos comes with python3 and changing the system python version is not recommended. I use pyenv to be able to use other python versions without modifing the system.
brew update
brew install pyenv
Add below to .zshrc. It makes sure pyenv is loaded so it works in terminal sessions. Whenever python command is invoked pyenv will be called and it will read .python-version file and load that version or a global version if defined.
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
Confirm installation:
pyenv --version

2. Use pyenv to set an project specific python version

2.1 Download a python version

install subcommand does not really install it into the system. It installs into a place ready to be called by pyenv.
pyenv install 3.11.0
To see a list of all python versions to install do:
pyenv install --list

2.2 Set version locally

Set a particular python version for a particular directory. Below example sets python 3.11.0 and to persist it, it will save it also in .python-version file in current directory.
cd path/to/project
pyenv local 3.11.0
Confirm local python version. Below commands should print 3.11.0.
cat .python-version
python --version

2.3 Set version globally (not project specific)

Set a particular python version for the entire machine. Internally it writes 3.9.1 into file ~/.pyenv/version. This file will be read by pyenv init - command when starting the shell.
pyenv global 3.9.1

3. Create an isolated environment

Different projects will have different dependencies and one project should not affect other. All they should be isolated. Since python 3.3 we can use venv module to easily create isolated versions.

3.1 Use venv to create an isolated environment

The following will create an environment named myenv hence a directory with such name will be created.
pyenv local 3.11.0
python -m venv myenv
Tip1
When creating the environment the current python version will be also recorded so make sure the desired python version is set prior.

Tip2
While myenv is a common choice, you can name it anything. Use meaningful names for your environments (e.g., dev, test, prod) to easily identify their purpose.

3.1.1 Activate the environment

Activating an environment means it becomes the isolated environment for our Python packages, so any `pip` installations affect only this environment without changing the global Python packages. To acchieve that `PATH` is updated and prompt is also change to reflect such change. Below command activates myenv environment.
source myenv/bin/activate

3.1.2 Deactivation

deactivate

3.1.3 Working with version control systems

Environment directory should not be commited so we should ignore them. This is an example of .gitignore file.
myenv/

3.2 [Old]Create an isolated environment with virtualenvwrapper

[Prior to python 3.3] virtualenv is a tool that creates isolated environments (in a directory) and the wrapper is as the name suggests a wrapper that helps centralizing this environments so we don't end up with lots of lots of boiler plate environment files in each project.
See old instructions... Install virtualenwrapper
pip install virtualenvwrapper
To be able to use functions of virtualenvwrapper we need to execute virtualenvwrapper.sh or virtualenvwrapper_lazy.sh first. So I add this to shell. In zsh it will be in ~/.zshrc
# Run `pip uninstall virtualenvwrapper` to find out real location of virtualenvwrapper
  # This might take varous seconds so disable this when not using python
  if [ -f .pyenv/versions/3.9.1/bin/virtualenvwrapper_lazy.sh ]; then
      export WORKON_HOME=$HOME/.pythonvirtualenvs # Optional
      # export PROJECT_HOME=$HOME/projects      # Optional
      export VIRTUALENVWRAPPER_PYTHON=$(which python) #Optional
      .pyenv/versions/3.9.1/bin/virtualenvwrapper_lazy.sh
  fi
  
Now some commands will he available. See more in other tutorials:
  • mkvirtualenv [your-env-name]
  • workon [your-env-name]
  • deactivate

4. Using python project.

Usually a project will like
  • project
  • ├── .python-version : file containing python version. Created with pyenv local ...)
  • ├── myenv/ : directory for isolated environment. Created with python -m venv myenv. Do not add to source control systems
  • ├── .gitignore : Source control system related file. It should ignore venv/ directory
  • ├── script.py : various scripts
  • └── requirements.txt : contains dependencies for current project
If you receive a project with this structure. And you have pyenv already installed (like step 1) then we only need to start the project
cd project
# Optional. Check python version, result should be the same as in .python-version file
python --version
# Activate isolated environment
source myenv/bin/activate
# Install all dependencies in isolated environment
pip install -r requirements.txt
That is all... I hope it helps.

0 comments :

This work is licensed under BSD Zero Clause License | nacho4d ®