Thursday, September 11, 2025

How to set up python environment for my project?

I came to python from java world. Initially, I struggled with setting up virtual environment for a new python project or for a new project I just git cloned. One of the reason being there are so many ways and it is so confusing ( pyenv, venv, poetry, virtualenv, pipenv, pip, conda, uv, pyenv-virtualenv.... ). Though lot of information is available on net, it still confusing because, I feel, most of these "How to..." posts miss very very basic information. So that prompted me to write this. 
Here I will show 2 ways. First one, I will be using pyenv. Second, I will be using uv. uv package is latest and suggest you to use it. 

Remember this. You can't set up python version and virtual environment everything in one go. So, one easy way is: First you need to take care of Python version you want to use. I use pyenv to manage Python on my laptop.

HERE IS 'A WAY'. 

When you get your MacBook, (apple stop giving default python since 12.3 ) first install a python. You can download from their official website or use Homebrew. I suggest using home brew, so it is easy to install or remove later. So once you have python install pyenv. Use brew again. Now use pyenv to manage the python version you need and set up environment. So if I do it today( 09/10/2025) I get python version 3.13.7. Let say I need 3.10.9 for my project, this is what I have to do.

( skip below 6 steps, if you have it already)

brew install python
brew install pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zprofile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zprofile
echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
echo 'eval "$(pyenv init -)"' >> ~/.zprofile



pyenv versions  <== list available python versions.
pyenv  install --list  <== list installed python versions.
pyenv install 3.10.9  <== installs specified python version.
cd ~/my_python_proj
pyenv local 3.10.9  <== this will create '.pyenv-version' file and set python version 
python -m venv .my_python_proj <== create virtual env; use proj name. easy to recognize
source .my_python_proj/bin/activate <== activate virtual env
which python  <== points to your python in your virtual env.
which pip  <== points to pip in your virtual env.
pip install request <== install any packages you want in your virtual env.
deactivate <== you will come out of virtual env 
pyenv global 3.13.7  <== this will be default python unless set with 'local'.
 


Now if you open this project in visual code studio, VS studio will 
automatically recognize the local python version and recommends you same. 

How to set up using uv?

uv is latest python package manager and is very fast. it is available as downloadable as well as python package. Don't try to install it as python package or download (using shell program). Install it as homebrew package. This way it is independent of python and also easy to upgrade/remove etc. 

brew install uv
uv venv --python 3.11 .venv
source .venv/bin/activate
deactivate






No comments:

Post a Comment