# Python: Python Package Manager - pipenv
# What is Environment in Computer ?
The Environment is the thing that defined why two rooms are different. Why would you choose to work at Library over your bedroom (or someone might choose otherwise). Why is your phone different from another phone. In UNIX, it is the same concept. Start from what package is installed, which version?. These are a part of Environment.
Reference: AIT - Software Architecture Design (opens new window)
# What is pipenv ?
pipenv
References: https://pipenv.pypa.io/en/latest/ (opens new window)
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.
# Installation
WARNING
You might install Python3 FIRST!
Secondly, please check the Python environment variable path For Windows.
# For UNIX family (MacOS, Ubuntu and Linux Distribution)
pip3 install pipenv
# For Windows
pip install pipenv
# Tutorial #1
# project structure
# Creates project directory
+-- root-path
| +-- project-1
2
# Creates .venv directory
+-- root-path
| +-- project-1
| +-- .venv
2
3
# Creates environment
cd /.../root-path/project-1
pipenv install
2
# project structure
+-- root-path
| +-- project-1
| +-- .venv
| +-- Pipfile
| +-- Pipfile.lock
2
3
4
5
pipenv install requests==2.28.1
cat Pipfile
output
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
requests = "==2.28.1"
[dev-packages]
[requires]
python_version = "3.10"
2
3
4
5
6
7
8
9
10
11
12
# Test Package
+-- root-path
| +-- project-1
| +-- .venv
| +-- Pipfile
| +-- Pipfile.lock
| +-- main.py
2
3
4
5
6
main.py
import requests
print(requests.__version__)
2
pipenv run python main.py
output
2.28.1
# Tutorial #2
+-- root-path
| +-- project-1
| +-- .venv
| +-- Pipfile
| +-- Pipfile.lock
| +-- main.py
| +-- project-2
| +-- .venv
2
3
4
5
6
7
8
cd /.../root-path/project-2
pipenv install numpy==1.23.1
2
+-- root-path
| +-- project-1
| +-- .venv
| +-- Pipfile
| +-- Pipfile.lock
| +-- main.py
| +-- project-2
| +-- .venv
| +-- Pipfile
| +-- Pipfile.lock
| +-- main.py
2
3
4
5
6
7
8
9
10
11
main.py
import numpy
print(numpy.__version__)
2
pipenv run python main.py
output
1.23.1
main.py
import numpy
print(numpy.__version__)
import requests
print(requests.__version__)
2
3
4
5
pipenv run python main.py
output
File "/.../root-path/project-2/main.py", line 4, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
2
3