SlideShare a Scribd company logo
Sebastian Witowski
Python Versions and
Dependencies Made Easy
Sebastian Witowski
Python consultant, freelancer, and trainer
@SebaWitowski switowski.com
Problem with (Python) projects
@SebaWitowski switowski.com
Problem with (Python) projects
Stuck in the 90s
Over-engineered
@SebaWitowski switowski.com
Problem with (Python) projects
Over-engineered
• Too many shiny new tools from
HackerNews

• Those tools gets abandoned

• You spend too much time
replacing old tools with new ones
Stuck in the 90s
@SebaWitowski switowski.com
Problem with (Python) projects
Over-engineered
• Too many shiny new tools from
HackerNews

• Those tools gets abandoned

• You spend too much time
replacing old tools with new ones
Stuck in the 90s
• Everything managed with Bash
scripts

• You have to maintain Bash
scripts or some other scripts
(as a Python developer)
@SebaWitowski switowski.com
How about a middle ground?
@SebaWitowski switowski.com
What will we talk about?
• pyenv - installing different Python versions on your computer
@SebaWitowski switowski.com
What will we talk about?
• pyenv - installing different Python versions on your computer

• venv - isolating dependencies of your projects
@SebaWitowski switowski.com
What will we talk about?
• pyenv - installing different Python versions on your computer

• venv - isolating dependencies of your projects 

• pipx - installing global packages
@SebaWitowski switowski.com
What will we talk about?
• pyenv - installing different Python versions on your computer

• venv - isolating dependencies of your projects 

• pipx - installing global packages

• pip-tools - dependency pinning
@SebaWitowski switowski.com
Installing Python
System Python
macOS
Ubuntu 20.04
Debian 10
@SebaWitowski
• Python version preinstalled on your OS is called “system Python”
System Python
@SebaWitowski switowski.com
• Python version preinstalled on your OS is called “system Python”

• Please, don’t use it
System Python
@SebaWitowski switowski.com
• Python version preinstalled on your OS is called “system Python”

• Please, don’t use it

• And don’t update it (you might break your OS)!
System Python
@SebaWitowski switowski.com
How to install a new Python version?
@SebaWitowski switowski.com
• Use an installer from python.org
How to install a new Python version?
@SebaWitowski switowski.com
• Use an installer from python.org

• Use your package manager
How to install a new Python version?
@SebaWitowski switowski.com
• Use an installer from python.org

• Use your package manager

• Compile it from the source files
How to install a new Python version?
@SebaWitowski switowski.com
Or you can use pyenv!
https://github.com/pyenv/pyenv
@SebaWitowski switowski.com
Or pyenv-win for Windows
https://github.com/pyenv-win/pyenv-win
@SebaWitowski switowski.com
Installing pyenv
@SebaWitowski switowski.com
Installing pyenv
@SebaWitowski switowski.com
Using pyenv
1. Find Python version to install
$ pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5.0
2.5.1
2.5.2
2.5.3
2.5.4
...
...
3.9.0
3.9-dev
3.10-dev
activepython-2.7.14
activepython-3.5.4
activepython-3.6.0
anaconda-1.4.0
anaconda-1.5.0
anaconda-1.5.1
anaconda-1.6.0
anaconda-1.6.1
anaconda-1.7.0
anaconda-1.8.0
anaconda-1.9.0
...
...
miniconda3-4.7.12
pypy-c-jit-latest
pypy-c-nojit-latest
pypy-dev
pypy-stm-2.3
pypy-stm-2.5.1
pypy-1.5-src
pypy-1.5
pypy-1.6
pypy-1.7
pypy-1.8
pypy-1.9
pypy-2.0-src
pypy-2.0
...
...
pypy3.6-7.3.0
pypy3.6-7.3.1-src
pypy3.6-7.3.1
pyston-0.5.1
pyston-0.6.0
pyston-0.6.1
stackless-dev
stackless-2.7-dev
stackless-2.7.2
stackless-2.7.3
stackless-2.7.4
stackless-2.7.5
stackless-2.7.6
...
@SebaWitowski switowski.com
Using pyenv
1. Find Python version to install
2. Install it
$ pyenv install 3.9.0
...
# Wait for a few minutes
...
# Done!
@SebaWitowski switowski.com
Using pyenv
1. Find Python version to install
2. Install it
3. Check if it’s installed correctly
$ pyenv versions
* system
3.9.0
@SebaWitowski switowski.com
Using pyenv
1. Find Python version to install
2. Install it
3. Check if it’s installed correctly
4. Start using this version
$ pyenv global 3.9.0
# or
$ pyenv local 3.9.0
# or
$ pyenv shell 3.9.0
@SebaWitowski switowski.com
3 levels of pyenv
• pyenv global 3.9.0 - changes the global Python version on your computer
@SebaWitowski switowski.com
3 levels of pyenv
• pyenv global 3.9.0 - changes the global Python version on your computer

• pyenv local 3.9.0 - changes Python version for the current folder and all the
sub-folders
@SebaWitowski switowski.com
3 levels of pyenv
• pyenv global 3.9.0 - changes the global Python version on your computer

• pyenv local 3.9.0 - changes Python version for the current folder and all the
sub-folders
# projectA
$ pyenv local 3.9.0
# projectB
$ pyenv local 3.6.0
@SebaWitowski switowski.com
3 levels of pyenv
• pyenv global 3.9.0 - changes the global Python version on your computer

• pyenv local 3.9.0 - changes Python version for the current folder and all the
sub-folders

• pyenv shell 2.7.18 - changes Python version for the current shell session
@SebaWitowski switowski.com
Problem with pip
You can only have one version of a given package installed!
@SebaWitowski switowski.com
$ pip install Django
...
Django 3.0 installed!
Problem with pip
@SebaWitowski switowski.com
Problem with pip
$ pip install Django==2.2
...
...
...
Django 2.2 installed!
@SebaWitowski switowski.com
$ pip install Django==2.2
...
Django==3.0 uninstalled!
...
Django 2.2 installed!
Problem with pip
@SebaWitowski switowski.com
# .../lib/python3.9/site-packages
$ ls -al
...
drwxr-xr-x 22 switowski 704 Oct 30 12:41 django/
drwxr-xr-x 12 switowski 384 Oct 30 12:41 django_redis/
drwxr-xr-x 16 switowski 512 Sep 28 17:58 docutils/
...
Problem with pip
@SebaWitowski switowski.com
# .../lib/python3.9/site-packages
$ ls -al
...
drwxr-xr-x 22 switowski 704 Oct 30 12:41 django/
drwxr-xr-x 12 switowski 384 Oct 30 12:41 django_redis/
drwxr-xr-x 16 switowski 512 Sep 28 17:58 docutils/
...
Problem with pip
@SebaWitowski switowski.com
Virtual environment
If pip installs every package in the same folder, why can’t we tell
it to temporarily install them in a different folder?
@SebaWitowski switowski.com
Virtual environment
If pip installs every package in the same folder, why can’t we tell
it to temporarily install them in a different folder?
Virtual environment:

• Creates a special folder with Python binaries

• Tells pip to install new packages to that folder

• Tells Python to use packages from that folder
@SebaWitowski switowski.com
Using virtual environments
1. Create new virtual environment
$ python -m venv .venv
@SebaWitowski switowski.com
Using virtual environments
1. Create new virtual environment
2. Activate it
$ source .venv/bin/activate
@SebaWitowski switowski.com
Using virtual environments
1. Create new virtual environment
2. Activate it
$ source .venv/bin/activate
# Your prompt should change:
(venv) $
@SebaWitowski switowski.com
Using virtual environments
1. Create new virtual environment
2. Activate it
3. Work on your Python project (install packages, run Python)
(venv) $ pip install Django==2.2
...
(venv) $ python manage.py runserver
@SebaWitowski switowski.com
Using virtual environments
1. Create new virtual environment
2. Activate it
3. Work on your Python project (install packages, run Python)
4. Deactivate virtual environment to stop using it
(venv) $ deactivate
$
@SebaWitowski switowski.com
Typical workflow
# Inside Django 3 project folder
$ python -m venv django3-app
$ source django3-app/bin/activate
$ pip install django==3.0
# Inside Django 2 project folder
$ python -m venv django2-app
$ source django2-app/bin/activate
$ pip install django==2.2
@SebaWitowski switowski.com
virtualenvwrapper
Windows users: https://pypi.org/project/virtualenvwrapper-win/
Linux and macOS:
@SebaWitowski switowski.com
virtualenvwrapper
virtualenvwrapper stores all virtual environments in:
~/.virtualenvs/
@SebaWitowski switowski.com
virtualenvwrapper
virtualenvwrapper stores all virtual environments in:
~/.virtualenvs/
$ mkvirtualenv django2-app - create a virtual environment
$ workon django2-app - activate it
$ lsvirtualenv - list all virtual environments
$ rmvirtualenv django2-app - delete a virtual environment
@SebaWitowski switowski.com
venv or virtualenvwrapper?
@SebaWitowski switowski.com
Global Python packages
• Some tools should be installed globally (black, flake8, virtualenvwrapper)
@SebaWitowski switowski.com
Global Python packages
• Some tools should be installed globally (black, flake8, virtualenvwrapper)

• But if they require different versions of the same dependency, one of them
will break
@SebaWitowski switowski.com
Global Python packages
• Some tools should be installed globally (black, flake8, virtualenvwrapper)

• But if they require different versions of the same dependency, one of them
will break

• Installing global tools into separate virtual environments is a hassle (you
have to activate each virtual environment to use that tool)
@SebaWitowski switowski.com
pipx - global packages in separate environments
https://github.com/pipxproject/pipx
@SebaWitowski switowski.com
pipx in action
1. Install “black” package
$ pipx install black
installed package black 20.8b1, Python 3.8.5
These apps are now globally available
- black
- black-primer
- blackd
done! ✨ 🌟 ✨
@SebaWitowski switowski.com
pipx in action
1. Install “black” package
2. Use black
$ black hello_world.py
reformatted hello_world.py
All done! ✨ 🍰 ✨
1 file reformatted.
@SebaWitowski switowski.com
pipx in action
1. Install “black” package
2. Use black
3. “black” installed in a virtual env will shadow the global one
# Inside a virtual environment
(venv) $ black --version
black, version 19.3b0
# Outside of a virtual environment
$ black --version
black, version 20.8b1
@SebaWitowski switowski.com
pipx commands
$ pipx list - list all installed packages
$ pipx uninstall <package> - uninstall package
$ pipx upgrade-all - upgrade all packages
$ pipx inject pytest pytest-cov - install pytest-cov inside
pytest virtual env
@SebaWitowski switowski.com
Dependencies in your project
@SebaWitowski switowski.com
Dependencies in your project
# requirements.txt
Django
django-redis
pytest
@SebaWitowski switowski.com
Dependencies in your project
# requirements.txt
Django
django-redis
pytest
$ pip install -r requirements.txt
...
Successfully installed Django-2.2, ...
@SebaWitowski switowski.com
Dependencies in your project
# requirements.txt
Django
django-redis
pytest
$ pip install -r requirements.txt
...
Successfully installed Django-2.2, ...
$ pip install -r requirements.txt
...
Successfully installed Django-3.0, ...
One month later…
Dependencies in your project
# requirements.txt
Django
django-redis
pytest
$ pip install -r requirements.txt
...
Successfully installed Django-2.2, ...
$ pip install -r requirements.txt
...
Successfully installed Django-3.0, ...
One month later…
Dependencies in your project
# requirements.txt
Django>=2.2,<3.0
django-redis==4.12.0
pytest==5.*
Dependency pinning:
@SebaWitowski switowski.com
Dependencies in your project
Always pin dependencies on production servers!
@SebaWitowski switowski.com
Dependencies in your project
Always pin dependencies on production servers!
And their sub-dependencies (3rd party packages)!
@SebaWitowski switowski.com
Dependencies in your project
Always pin dependencies on production servers!
And their sub-dependencies (3rd party packages)!
And don’t forget to update packages regularly!
@SebaWitowski switowski.com
Dependencies in your project
Always pin dependencies on production servers!
And their sub-dependencies (3rd party packages)!
And don’t forget to update packages regularly!
That’s a lot of work!
@SebaWitowski switowski.com
Pipenv and Poetry to the rescue!
But do you really need them?
@SebaWitowski switowski.com
But do you really need them?
• What if they stop working?
@SebaWitowski switowski.com
But do you really need them?
• What if they stop working?

• What if they get discontinued?
@SebaWitowski switowski.com
But do you really need them?
• What if they stop working?

• What if they get discontinued?

• Do you really use ALL their features?
@SebaWitowski switowski.com
Pipenv and Poetry are great
@SebaWitowski switowski.com
But sometimes a simpler tool is enough
Pipenv and Poetry are great
@SebaWitowski switowski.com
But sometimes a simpler tool is enough
Pipenv and Poetry are great
https://github.com/jazzband/pip-tools
@SebaWitowski switowski.com
pip-compile
@SebaWitowski switowski.com
pip-compile
# requirements.in
Django>=2.2>,<3.0
pytest
# requirements.txt
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements.in
#
attrs==20.2.0 # via pytest
django==2.2.17 # via -r requirements.in
importlib-metadata==2.0.0 # via pluggy, pytest
iniconfig==1.1.1 # via pytest
packaging==20.4 # via pytest
pluggy==0.13.1 # via pytest
py==1.9.0 # via pytest
pyparsing==2.4.7 # via packaging
pytest==6.1.2 # via -r requirements.in
pytz==2020.4 # via django
six==1.15.0 # via packaging
sqlparse==0.4.1 # via django
toml==0.10.2 # via pytest
zipp==3.4.0 # via importlib-metadata
$ pip-compile requirements.in
pip-compile
# requirements.txt
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements.in
#
attrs==20.2.0 # via pytest
django==2.2.17 # via -r requirements.in
importlib-metadata==2.0.0 # via pluggy, pytest
iniconfig==1.1.1 # via pytest
packaging==20.4 # via pytest
pluggy==0.13.1 # via pytest
py==1.9.0 # via pytest
pyparsing==2.4.7 # via packaging
pytest==6.1.2 # via -r requirements.in
pytz==2020.4 # via django
six==1.15.0 # via packaging
sqlparse==0.4.1 # via django
toml==0.10.2 # via pytest
zipp==3.4.0 # via importlib-metadata
@SebaWitowski switowski.com
pip-compile
# requirements.txt
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements.in
#
attrs==20.2.0 # via pytest
django==2.2.17 # via -r requirements.in
importlib-metadata==2.0.0 # via pluggy, pytest
iniconfig==1.1.1 # via pytest
packaging==20.4 # via pytest
pluggy==0.13.1 # via pytest
py==1.9.0 # via pytest
pyparsing==2.4.7 # via packaging
pytest==6.1.2 # via -r requirements.in
pytz==2020.4 # via django
six==1.15.0 # via packaging
sqlparse==0.4.1 # via django
toml==0.10.2 # via pytest
zipp==3.4.0 # via importlib-metadata
@SebaWitowski switowski.com
pip-compile
# requirements.txt
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements.in
#
attrs==20.2.0 # via pytest
django==2.2.17 # via -r requirements.in
importlib-metadata==2.0.0 # via pluggy, pytest
iniconfig==1.1.1 # via pytest
packaging==20.4 # via pytest
pluggy==0.13.1 # via pytest
py==1.9.0 # via pytest
pyparsing==2.4.7 # via packaging
pytest==6.1.2 # via -r requirements.in
pytz==2020.4 # via django
six==1.15.0 # via packaging
sqlparse==0.4.1 # via django
toml==0.10.2 # via pytest
zipp==3.4.0 # via importlib-metadata
@SebaWitowski switowski.com
pip-compile
# requirements.txt
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements.in
#
attrs==20.2.0 # via pytest
django==2.2.17 # via -r requirements.in
importlib-metadata==2.0.0 # via pluggy, pytest
iniconfig==1.1.1 # via pytest
packaging==20.4 # via pytest
pluggy==0.13.1 # via pytest
py==1.9.0 # via pytest
pyparsing==2.4.7 # via packaging
pytest==6.1.2 # via -r requirements.in
pytz==2020.4 # via django
six==1.15.0 # via packaging
sqlparse==0.4.1 # via django
toml==0.10.2 # via pytest
zipp==3.4.0 # via importlib-metadata
@SebaWitowski switowski.com
pip-compile
# requirements.txt
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile requirements.in
#
attrs==20.2.0 # via pytest
django==2.2.17 # via -r requirements.in
importlib-metadata==2.0.0 # via pluggy, pytest
iniconfig==1.1.1 # via pytest
packaging==20.4 # via pytest
pluggy==0.13.1 # via pytest
py==1.9.0 # via pytest
pyparsing==2.4.7 # via packaging
pytest==6.1.2 # via -r requirements.in
pytz==2020.4 # via django
six==1.15.0 # via packaging
sqlparse==0.4.1 # via django
toml==0.10.2 # via pytest
zipp==3.4.0 # via importlib-metadata
@SebaWitowski switowski.com
pip-compile
Now, run:
$ pip install -r requirements.txt
@SebaWitowski switowski.com
pip-compile
Now, run:
$ pip install -r requirements.txt
Need to update dependencies?
$ pip-compile requirements.in
@SebaWitowski switowski.com
pip-compile
Make pip-compile part of your CI
@SebaWitowski switowski.com
pip-compile
Make pip-compile part of your CI
If something breaks, pin that package in requirements.in
@SebaWitowski switowski.com
pip-sync
@SebaWitowski switowski.com
pip-tools
vs.
Pipenv and Poetry
@SebaWitowski switowski.com
Summary
@SebaWitowski switowski.com
Summary
• Use pyenv to install/update Python versions
@SebaWitowski switowski.com
Summary
• Use pyenv to install/update Python versions
• Use venv/virtualenvwrapper to isolate dependencies of your projects
@SebaWitowski switowski.com
Summary
• Use pyenv to install/update Python versions
• Use venv/virtualenvwrapper to isolate dependencies of your projects
• Use pipx to install global tools
@SebaWitowski switowski.com
Summary
• Use pyenv to install/update Python versions
• Use venv/virtualenvwrapper to isolate dependencies of your projects
• Use pipx to install global tools
• Consider pip-tools when you just need to pin dependencies
@SebaWitowski switowski.com
Where to go next?
@SebaWitowski switowski.com
Where to go next?
“Modern Python Projects” workshop (part of this conference)
@SebaWitowski switowski.com
Where to go next?
“Modern Python Projects” online course
https://modernpythonprojects.com/
@SebaWitowski switowski.com

More Related Content

PPT
Prognosis
PPTX
Biomarkers of Periodontal Diseases
PPTX
Computer Applications and its use in Dentistry.pptx
PPTX
Saliva - A diagnostic marker ; Dr Harshavardhan Patwal
PPTX
Genetic factors
PDF
PIAZZA株式会社_採用ブック_202108
PPTX
ChatGPT以後の時代をどう生きるか PWA Night vol.51
Prognosis
Biomarkers of Periodontal Diseases
Computer Applications and its use in Dentistry.pptx
Saliva - A diagnostic marker ; Dr Harshavardhan Patwal
Genetic factors
PIAZZA株式会社_採用ブック_202108
ChatGPT以後の時代をどう生きるか PWA Night vol.51

What's hot (20)

PPT
Porphyromonas gingivalis - Dr Harshavardhan Patwal
PPT
Ultrastr of gingiva
PPTX
PDF
Turning diligence insights into actionable integration steps
PDF
021.acute gingival diseases
PPTX
Innate immunity in periodontics
PPTX
Development of periodontium. periodontics
PDF
Graph Database Meetup in Korea #6. Graph Database 5 Offerings_ AssetManager (...
PPTX
hormonal influences on periodontium
PPTX
Periodontal tharapy in female patients
PDF
Decoding Transformation
PPTX
スケジュール遅延が当たり前な状況を少し良くしたいチームがその未来のためにScrumに”再”挑戦した話
PPTX
EXTENT AND SEVERITY INDEX (ESI).pptx
PPTX
Gingival crevicular fluid
PPTX
Risk factors
PPTX
Surgical anatomy of periodontal structures,
PPTX
p.gingivalis
PDF
いともたやすく行われるえげつない研究行為
PPTX
Rationale for use of antibiotics after periodontal surgery
PPTX
Hiv and periodontium
Porphyromonas gingivalis - Dr Harshavardhan Patwal
Ultrastr of gingiva
Turning diligence insights into actionable integration steps
021.acute gingival diseases
Innate immunity in periodontics
Development of periodontium. periodontics
Graph Database Meetup in Korea #6. Graph Database 5 Offerings_ AssetManager (...
hormonal influences on periodontium
Periodontal tharapy in female patients
Decoding Transformation
スケジュール遅延が当たり前な状況を少し良くしたいチームがその未来のためにScrumに”再”挑戦した話
EXTENT AND SEVERITY INDEX (ESI).pptx
Gingival crevicular fluid
Risk factors
Surgical anatomy of periodontal structures,
p.gingivalis
いともたやすく行われるえげつない研究行為
Rationale for use of antibiotics after periodontal surgery
Hiv and periodontium
Ad

Similar to Python Versions and Dependencies Made Easy (20)

PPTX
Python Programming-Lesson 1- Installation and Environmental Set-up.pptx
PDF
Django Dev Environment Howto
PPTX
Run Python on windows
PDF
Virtualenv
PPTX
Python Course
PDF
Effectively using Open Source with conda
PDF
Py Day Mallorca - Pipenv - Python Dev Workflow for Humans
PDF
Pip + virtualenv
PPTX
Introduction Of Python History And Features Of Python, Installing Python And ...
PPTX
Introduction Of Python History And Features Of Python..pptx
PPTX
Introduction of Python History and Features of Python, Installing Python and ...
PDF
A Whirlwind Tour Of Python
PPTX
Python 1
PPTX
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
PDF
Packaging in Python? Don't Roll the Dice.
PPTX
Pipenv - The Python Companion You Wish You Always Had
PDF
venv and pip.pdf
PDF
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
PDF
Python tutorial
PPTX
Introduction to Python Programming
Python Programming-Lesson 1- Installation and Environmental Set-up.pptx
Django Dev Environment Howto
Run Python on windows
Virtualenv
Python Course
Effectively using Open Source with conda
Py Day Mallorca - Pipenv - Python Dev Workflow for Humans
Pip + virtualenv
Introduction Of Python History And Features Of Python, Installing Python And ...
Introduction Of Python History And Features Of Python..pptx
Introduction of Python History and Features of Python, Installing Python and ...
A Whirlwind Tour Of Python
Python 1
Python Mastery: A Comprehensive Guide to Setting Up Your Development Environment
Packaging in Python? Don't Roll the Dice.
Pipenv - The Python Companion You Wish You Always Had
venv and pip.pdf
Christian Strappazzon - Presentazione Python Milano - Codemotion Milano 2017
Python tutorial
Introduction to Python Programming
Ad

More from Sebastian Witowski (7)

PDF
5 Things I Wish I Knew About Gitlab CI
PDF
Optimizing Your CI Pipelines
PDF
Writing Faster Python 3
PDF
Productivity tips for developers
PDF
Wait, IPython can do that?! (30 minutes)
PDF
It's 2019 & I'm still using Python 2! Should I be worried?
PDF
Wait, IPython can do that?
5 Things I Wish I Knew About Gitlab CI
Optimizing Your CI Pipelines
Writing Faster Python 3
Productivity tips for developers
Wait, IPython can do that?! (30 minutes)
It's 2019 & I'm still using Python 2! Should I be worried?
Wait, IPython can do that?

Recently uploaded (20)

PPTX
ai tools demonstartion for schools and inter college
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
medical staffing services at VALiNTRY
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PPTX
Essential Infomation Tech presentation.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
AI in Product Development-omnex systems
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Introduction to Artificial Intelligence
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
ai tools demonstartion for schools and inter college
PTS Company Brochure 2025 (1).pdf.......
medical staffing services at VALiNTRY
Which alternative to Crystal Reports is best for small or large businesses.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms II-SECS-1021-03
Digital Strategies for Manufacturing Companies
Essential Infomation Tech presentation.pptx
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
AI in Product Development-omnex systems
Softaken Excel to vCard Converter Software.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Introduction to Artificial Intelligence
How to Choose the Right IT Partner for Your Business in Malaysia
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

Python Versions and Dependencies Made Easy

  • 1. Sebastian Witowski Python Versions and Dependencies Made Easy
  • 2. Sebastian Witowski Python consultant, freelancer, and trainer @SebaWitowski switowski.com
  • 3. Problem with (Python) projects @SebaWitowski switowski.com
  • 4. Problem with (Python) projects Stuck in the 90s Over-engineered @SebaWitowski switowski.com
  • 5. Problem with (Python) projects Over-engineered • Too many shiny new tools from HackerNews • Those tools gets abandoned • You spend too much time replacing old tools with new ones Stuck in the 90s @SebaWitowski switowski.com
  • 6. Problem with (Python) projects Over-engineered • Too many shiny new tools from HackerNews • Those tools gets abandoned • You spend too much time replacing old tools with new ones Stuck in the 90s • Everything managed with Bash scripts • You have to maintain Bash scripts or some other scripts (as a Python developer) @SebaWitowski switowski.com
  • 7. How about a middle ground? @SebaWitowski switowski.com
  • 8. What will we talk about? • pyenv - installing different Python versions on your computer @SebaWitowski switowski.com
  • 9. What will we talk about? • pyenv - installing different Python versions on your computer • venv - isolating dependencies of your projects @SebaWitowski switowski.com
  • 10. What will we talk about? • pyenv - installing different Python versions on your computer • venv - isolating dependencies of your projects • pipx - installing global packages @SebaWitowski switowski.com
  • 11. What will we talk about? • pyenv - installing different Python versions on your computer • venv - isolating dependencies of your projects • pipx - installing global packages • pip-tools - dependency pinning @SebaWitowski switowski.com
  • 14. • Python version preinstalled on your OS is called “system Python” System Python @SebaWitowski switowski.com
  • 15. • Python version preinstalled on your OS is called “system Python” • Please, don’t use it System Python @SebaWitowski switowski.com
  • 16. • Python version preinstalled on your OS is called “system Python” • Please, don’t use it • And don’t update it (you might break your OS)! System Python @SebaWitowski switowski.com
  • 17. How to install a new Python version? @SebaWitowski switowski.com
  • 18. • Use an installer from python.org How to install a new Python version? @SebaWitowski switowski.com
  • 19. • Use an installer from python.org • Use your package manager How to install a new Python version? @SebaWitowski switowski.com
  • 20. • Use an installer from python.org • Use your package manager • Compile it from the source files How to install a new Python version? @SebaWitowski switowski.com
  • 21. Or you can use pyenv! https://github.com/pyenv/pyenv @SebaWitowski switowski.com
  • 22. Or pyenv-win for Windows https://github.com/pyenv-win/pyenv-win @SebaWitowski switowski.com
  • 25. Using pyenv 1. Find Python version to install $ pyenv install --list Available versions: 2.1.3 2.2.3 2.3.7 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 ... ... 3.9.0 3.9-dev 3.10-dev activepython-2.7.14 activepython-3.5.4 activepython-3.6.0 anaconda-1.4.0 anaconda-1.5.0 anaconda-1.5.1 anaconda-1.6.0 anaconda-1.6.1 anaconda-1.7.0 anaconda-1.8.0 anaconda-1.9.0 ... ... miniconda3-4.7.12 pypy-c-jit-latest pypy-c-nojit-latest pypy-dev pypy-stm-2.3 pypy-stm-2.5.1 pypy-1.5-src pypy-1.5 pypy-1.6 pypy-1.7 pypy-1.8 pypy-1.9 pypy-2.0-src pypy-2.0 ... ... pypy3.6-7.3.0 pypy3.6-7.3.1-src pypy3.6-7.3.1 pyston-0.5.1 pyston-0.6.0 pyston-0.6.1 stackless-dev stackless-2.7-dev stackless-2.7.2 stackless-2.7.3 stackless-2.7.4 stackless-2.7.5 stackless-2.7.6 ... @SebaWitowski switowski.com
  • 26. Using pyenv 1. Find Python version to install 2. Install it $ pyenv install 3.9.0 ... # Wait for a few minutes ... # Done! @SebaWitowski switowski.com
  • 27. Using pyenv 1. Find Python version to install 2. Install it 3. Check if it’s installed correctly $ pyenv versions * system 3.9.0 @SebaWitowski switowski.com
  • 28. Using pyenv 1. Find Python version to install 2. Install it 3. Check if it’s installed correctly 4. Start using this version $ pyenv global 3.9.0 # or $ pyenv local 3.9.0 # or $ pyenv shell 3.9.0 @SebaWitowski switowski.com
  • 29. 3 levels of pyenv • pyenv global 3.9.0 - changes the global Python version on your computer @SebaWitowski switowski.com
  • 30. 3 levels of pyenv • pyenv global 3.9.0 - changes the global Python version on your computer • pyenv local 3.9.0 - changes Python version for the current folder and all the sub-folders @SebaWitowski switowski.com
  • 31. 3 levels of pyenv • pyenv global 3.9.0 - changes the global Python version on your computer • pyenv local 3.9.0 - changes Python version for the current folder and all the sub-folders # projectA $ pyenv local 3.9.0 # projectB $ pyenv local 3.6.0 @SebaWitowski switowski.com
  • 32. 3 levels of pyenv • pyenv global 3.9.0 - changes the global Python version on your computer • pyenv local 3.9.0 - changes Python version for the current folder and all the sub-folders • pyenv shell 2.7.18 - changes Python version for the current shell session @SebaWitowski switowski.com
  • 33. Problem with pip You can only have one version of a given package installed! @SebaWitowski switowski.com
  • 34. $ pip install Django ... Django 3.0 installed! Problem with pip @SebaWitowski switowski.com
  • 35. Problem with pip $ pip install Django==2.2 ... ... ... Django 2.2 installed! @SebaWitowski switowski.com
  • 36. $ pip install Django==2.2 ... Django==3.0 uninstalled! ... Django 2.2 installed! Problem with pip @SebaWitowski switowski.com
  • 37. # .../lib/python3.9/site-packages $ ls -al ... drwxr-xr-x 22 switowski 704 Oct 30 12:41 django/ drwxr-xr-x 12 switowski 384 Oct 30 12:41 django_redis/ drwxr-xr-x 16 switowski 512 Sep 28 17:58 docutils/ ... Problem with pip @SebaWitowski switowski.com
  • 38. # .../lib/python3.9/site-packages $ ls -al ... drwxr-xr-x 22 switowski 704 Oct 30 12:41 django/ drwxr-xr-x 12 switowski 384 Oct 30 12:41 django_redis/ drwxr-xr-x 16 switowski 512 Sep 28 17:58 docutils/ ... Problem with pip @SebaWitowski switowski.com
  • 39. Virtual environment If pip installs every package in the same folder, why can’t we tell it to temporarily install them in a different folder? @SebaWitowski switowski.com
  • 40. Virtual environment If pip installs every package in the same folder, why can’t we tell it to temporarily install them in a different folder? Virtual environment: • Creates a special folder with Python binaries • Tells pip to install new packages to that folder • Tells Python to use packages from that folder @SebaWitowski switowski.com
  • 41. Using virtual environments 1. Create new virtual environment $ python -m venv .venv @SebaWitowski switowski.com
  • 42. Using virtual environments 1. Create new virtual environment 2. Activate it $ source .venv/bin/activate @SebaWitowski switowski.com
  • 43. Using virtual environments 1. Create new virtual environment 2. Activate it $ source .venv/bin/activate # Your prompt should change: (venv) $ @SebaWitowski switowski.com
  • 44. Using virtual environments 1. Create new virtual environment 2. Activate it 3. Work on your Python project (install packages, run Python) (venv) $ pip install Django==2.2 ... (venv) $ python manage.py runserver @SebaWitowski switowski.com
  • 45. Using virtual environments 1. Create new virtual environment 2. Activate it 3. Work on your Python project (install packages, run Python) 4. Deactivate virtual environment to stop using it (venv) $ deactivate $ @SebaWitowski switowski.com
  • 46. Typical workflow # Inside Django 3 project folder $ python -m venv django3-app $ source django3-app/bin/activate $ pip install django==3.0 # Inside Django 2 project folder $ python -m venv django2-app $ source django2-app/bin/activate $ pip install django==2.2 @SebaWitowski switowski.com
  • 48. virtualenvwrapper virtualenvwrapper stores all virtual environments in: ~/.virtualenvs/ @SebaWitowski switowski.com
  • 49. virtualenvwrapper virtualenvwrapper stores all virtual environments in: ~/.virtualenvs/ $ mkvirtualenv django2-app - create a virtual environment $ workon django2-app - activate it $ lsvirtualenv - list all virtual environments $ rmvirtualenv django2-app - delete a virtual environment @SebaWitowski switowski.com
  • 51. Global Python packages • Some tools should be installed globally (black, flake8, virtualenvwrapper) @SebaWitowski switowski.com
  • 52. Global Python packages • Some tools should be installed globally (black, flake8, virtualenvwrapper) • But if they require different versions of the same dependency, one of them will break @SebaWitowski switowski.com
  • 53. Global Python packages • Some tools should be installed globally (black, flake8, virtualenvwrapper) • But if they require different versions of the same dependency, one of them will break • Installing global tools into separate virtual environments is a hassle (you have to activate each virtual environment to use that tool) @SebaWitowski switowski.com
  • 54. pipx - global packages in separate environments https://github.com/pipxproject/pipx @SebaWitowski switowski.com
  • 55. pipx in action 1. Install “black” package $ pipx install black installed package black 20.8b1, Python 3.8.5 These apps are now globally available - black - black-primer - blackd done! ✨ 🌟 ✨ @SebaWitowski switowski.com
  • 56. pipx in action 1. Install “black” package 2. Use black $ black hello_world.py reformatted hello_world.py All done! ✨ 🍰 ✨ 1 file reformatted. @SebaWitowski switowski.com
  • 57. pipx in action 1. Install “black” package 2. Use black 3. “black” installed in a virtual env will shadow the global one # Inside a virtual environment (venv) $ black --version black, version 19.3b0 # Outside of a virtual environment $ black --version black, version 20.8b1 @SebaWitowski switowski.com
  • 58. pipx commands $ pipx list - list all installed packages $ pipx uninstall <package> - uninstall package $ pipx upgrade-all - upgrade all packages $ pipx inject pytest pytest-cov - install pytest-cov inside pytest virtual env @SebaWitowski switowski.com
  • 59. Dependencies in your project @SebaWitowski switowski.com
  • 60. Dependencies in your project # requirements.txt Django django-redis pytest @SebaWitowski switowski.com
  • 61. Dependencies in your project # requirements.txt Django django-redis pytest $ pip install -r requirements.txt ... Successfully installed Django-2.2, ... @SebaWitowski switowski.com
  • 62. Dependencies in your project # requirements.txt Django django-redis pytest $ pip install -r requirements.txt ... Successfully installed Django-2.2, ... $ pip install -r requirements.txt ... Successfully installed Django-3.0, ... One month later…
  • 63. Dependencies in your project # requirements.txt Django django-redis pytest $ pip install -r requirements.txt ... Successfully installed Django-2.2, ... $ pip install -r requirements.txt ... Successfully installed Django-3.0, ... One month later…
  • 64. Dependencies in your project # requirements.txt Django>=2.2,<3.0 django-redis==4.12.0 pytest==5.* Dependency pinning: @SebaWitowski switowski.com
  • 65. Dependencies in your project Always pin dependencies on production servers! @SebaWitowski switowski.com
  • 66. Dependencies in your project Always pin dependencies on production servers! And their sub-dependencies (3rd party packages)! @SebaWitowski switowski.com
  • 67. Dependencies in your project Always pin dependencies on production servers! And their sub-dependencies (3rd party packages)! And don’t forget to update packages regularly! @SebaWitowski switowski.com
  • 68. Dependencies in your project Always pin dependencies on production servers! And their sub-dependencies (3rd party packages)! And don’t forget to update packages regularly! That’s a lot of work! @SebaWitowski switowski.com
  • 69. Pipenv and Poetry to the rescue!
  • 70. But do you really need them? @SebaWitowski switowski.com
  • 71. But do you really need them? • What if they stop working? @SebaWitowski switowski.com
  • 72. But do you really need them? • What if they stop working? • What if they get discontinued? @SebaWitowski switowski.com
  • 73. But do you really need them? • What if they stop working? • What if they get discontinued? • Do you really use ALL their features? @SebaWitowski switowski.com
  • 74. Pipenv and Poetry are great @SebaWitowski switowski.com
  • 75. But sometimes a simpler tool is enough Pipenv and Poetry are great @SebaWitowski switowski.com
  • 76. But sometimes a simpler tool is enough Pipenv and Poetry are great https://github.com/jazzband/pip-tools @SebaWitowski switowski.com
  • 78. pip-compile # requirements.in Django>=2.2>,<3.0 pytest # requirements.txt # # This file is autogenerated by pip-compile # To update, run: # # pip-compile requirements.in # attrs==20.2.0 # via pytest django==2.2.17 # via -r requirements.in importlib-metadata==2.0.0 # via pluggy, pytest iniconfig==1.1.1 # via pytest packaging==20.4 # via pytest pluggy==0.13.1 # via pytest py==1.9.0 # via pytest pyparsing==2.4.7 # via packaging pytest==6.1.2 # via -r requirements.in pytz==2020.4 # via django six==1.15.0 # via packaging sqlparse==0.4.1 # via django toml==0.10.2 # via pytest zipp==3.4.0 # via importlib-metadata $ pip-compile requirements.in
  • 79. pip-compile # requirements.txt # # This file is autogenerated by pip-compile # To update, run: # # pip-compile requirements.in # attrs==20.2.0 # via pytest django==2.2.17 # via -r requirements.in importlib-metadata==2.0.0 # via pluggy, pytest iniconfig==1.1.1 # via pytest packaging==20.4 # via pytest pluggy==0.13.1 # via pytest py==1.9.0 # via pytest pyparsing==2.4.7 # via packaging pytest==6.1.2 # via -r requirements.in pytz==2020.4 # via django six==1.15.0 # via packaging sqlparse==0.4.1 # via django toml==0.10.2 # via pytest zipp==3.4.0 # via importlib-metadata @SebaWitowski switowski.com
  • 80. pip-compile # requirements.txt # # This file is autogenerated by pip-compile # To update, run: # # pip-compile requirements.in # attrs==20.2.0 # via pytest django==2.2.17 # via -r requirements.in importlib-metadata==2.0.0 # via pluggy, pytest iniconfig==1.1.1 # via pytest packaging==20.4 # via pytest pluggy==0.13.1 # via pytest py==1.9.0 # via pytest pyparsing==2.4.7 # via packaging pytest==6.1.2 # via -r requirements.in pytz==2020.4 # via django six==1.15.0 # via packaging sqlparse==0.4.1 # via django toml==0.10.2 # via pytest zipp==3.4.0 # via importlib-metadata @SebaWitowski switowski.com
  • 81. pip-compile # requirements.txt # # This file is autogenerated by pip-compile # To update, run: # # pip-compile requirements.in # attrs==20.2.0 # via pytest django==2.2.17 # via -r requirements.in importlib-metadata==2.0.0 # via pluggy, pytest iniconfig==1.1.1 # via pytest packaging==20.4 # via pytest pluggy==0.13.1 # via pytest py==1.9.0 # via pytest pyparsing==2.4.7 # via packaging pytest==6.1.2 # via -r requirements.in pytz==2020.4 # via django six==1.15.0 # via packaging sqlparse==0.4.1 # via django toml==0.10.2 # via pytest zipp==3.4.0 # via importlib-metadata @SebaWitowski switowski.com
  • 82. pip-compile # requirements.txt # # This file is autogenerated by pip-compile # To update, run: # # pip-compile requirements.in # attrs==20.2.0 # via pytest django==2.2.17 # via -r requirements.in importlib-metadata==2.0.0 # via pluggy, pytest iniconfig==1.1.1 # via pytest packaging==20.4 # via pytest pluggy==0.13.1 # via pytest py==1.9.0 # via pytest pyparsing==2.4.7 # via packaging pytest==6.1.2 # via -r requirements.in pytz==2020.4 # via django six==1.15.0 # via packaging sqlparse==0.4.1 # via django toml==0.10.2 # via pytest zipp==3.4.0 # via importlib-metadata @SebaWitowski switowski.com
  • 83. pip-compile # requirements.txt # # This file is autogenerated by pip-compile # To update, run: # # pip-compile requirements.in # attrs==20.2.0 # via pytest django==2.2.17 # via -r requirements.in importlib-metadata==2.0.0 # via pluggy, pytest iniconfig==1.1.1 # via pytest packaging==20.4 # via pytest pluggy==0.13.1 # via pytest py==1.9.0 # via pytest pyparsing==2.4.7 # via packaging pytest==6.1.2 # via -r requirements.in pytz==2020.4 # via django six==1.15.0 # via packaging sqlparse==0.4.1 # via django toml==0.10.2 # via pytest zipp==3.4.0 # via importlib-metadata @SebaWitowski switowski.com
  • 84. pip-compile Now, run: $ pip install -r requirements.txt @SebaWitowski switowski.com
  • 85. pip-compile Now, run: $ pip install -r requirements.txt Need to update dependencies? $ pip-compile requirements.in @SebaWitowski switowski.com
  • 86. pip-compile Make pip-compile part of your CI @SebaWitowski switowski.com
  • 87. pip-compile Make pip-compile part of your CI If something breaks, pin that package in requirements.in @SebaWitowski switowski.com
  • 91. Summary • Use pyenv to install/update Python versions @SebaWitowski switowski.com
  • 92. Summary • Use pyenv to install/update Python versions • Use venv/virtualenvwrapper to isolate dependencies of your projects @SebaWitowski switowski.com
  • 93. Summary • Use pyenv to install/update Python versions • Use venv/virtualenvwrapper to isolate dependencies of your projects • Use pipx to install global tools @SebaWitowski switowski.com
  • 94. Summary • Use pyenv to install/update Python versions • Use venv/virtualenvwrapper to isolate dependencies of your projects • Use pipx to install global tools • Consider pip-tools when you just need to pin dependencies @SebaWitowski switowski.com
  • 95. Where to go next? @SebaWitowski switowski.com
  • 96. Where to go next? “Modern Python Projects” workshop (part of this conference) @SebaWitowski switowski.com
  • 97. Where to go next? “Modern Python Projects” online course https://modernpythonprojects.com/ @SebaWitowski switowski.com