How to install Python packages with pipenv, venv or virtualenv package manager

You can find GitHub repository in https://github.com/python-projects-kenanhancer/python_demo

  • Pyenv – Python Runtime Version Manager
    – to install, uninstall, list or switch between different python runtimes
  • Pipenv – Python Package Manager and Virtual environment manager
    – to create an isolated virtual environment with a specific python runtime
    – to install, uninstall, list python packages
  • Venv – Virtual environment manager (for Python 3)
    – to create an isolated virtual environment with a specific python runtime
    – python packages will be isolated already
  • Virtualenv – Virtual environment manager (for Python 2)
    – to create an isolated virtual environment with a specific python runtime
    – python packages will be isolated already

Installing Python Runtime Versions via Pyenv

In order to test different Python Runtime versions, I have installed some versions.

$ pyenv install 3.10.2
$ pyenv install 3.10.3
$ pyenv install 3.10.4
$ pyenv install 3.10.5

I have 4 Python runtime versions(3.10.2, 3.10.3, 3.10.4, 3.10.5) in my machine. So, let's list them.

$ pyenv versions

  system
  3.10.2
  3.10.3
  3.10.4
* 3.10.5 (set by /Users/kenanhancer/.pyenv/version)

Creating a new project

$ mkdir python_demo1
$ cd python_demo1
$ pyenv local 3.10.2
$ pyenv version

3.10.2 (set by /Users/kenanhancer/Documents/projects/python_demo1/.python-version)
$ python -m pip list

Package    Version
---------- -------
pip        21.2.4
setuptools 58.1.0
WARNING: You are using pip version 21.2.4; however, version 22.2 is available.
You should consider upgrading via the '/Users/kenanhancer/.pyenv/versions/3.10.2/bin/python -m pip install --upgrade pip' command.
$ python -m pip install --upgrade pip

Requirement already satisfied: pip in /Users/kenanhancer/.pyenv/versions/3.10.2/lib/python3.10/site-packages (21.2.4)
Collecting pip
  Using cached pip-22.2-py3-none-any.whl (2.0 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.4
    Uninstalling pip-21.2.4:
      Successfully uninstalled pip-21.2.4
Successfully installed pip-22.2
$ python -m pip list

Package    Version
---------- -------
pip        22.2
setuptools 58.1.0

Creating virtual environment via Pipenv

$ python -m pip install pipenv

Collecting pipenv
  Downloading pipenv-2022.7.24-py2.py3-none-any.whl (3.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.7/3.7 MB 12.3 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=36.2.1 in /Users/kenanhancer/.pyenv/versions/3.10.2/lib/python3.10/site-packages (from pipenv) (58.1.0)
Collecting virtualenv-clone>=0.2.5
  Using cached virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB)
Requirement already satisfied: pip>=22.0.4 in /Users/kenanhancer/.pyenv/versions/3.10.2/lib/python3.10/site-packages (from pipenv) (22.2)
Collecting virtualenv
  Using cached virtualenv-20.15.1-py2.py3-none-any.whl (10.1 MB)
Collecting certifi
  Using cached certifi-2022.6.15-py3-none-any.whl (160 kB)
Collecting platformdirs<3,>=2
  Using cached platformdirs-2.5.2-py3-none-any.whl (14 kB)
Collecting six<2,>=1.9.0
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting distlib<1,>=0.3.1
  Using cached distlib-0.3.5-py2.py3-none-any.whl (466 kB)
Collecting filelock<4,>=3.2
  Using cached filelock-3.7.1-py3-none-any.whl (10 kB)
Installing collected packages: distlib, virtualenv-clone, six, platformdirs, filelock, certifi, virtualenv, pipenv
Successfully installed certifi-2022.6.15 distlib-0.3.5 filelock-3.7.1 pipenv-2022.7.24 platformdirs-2.5.2 six-1.16.0 virtualenv-20.15.1 virtualenv-clone-0.5.7
$ python -m pip list

Package          Version
---------------- ---------
certifi          2022.6.15
distlib          0.3.5
filelock         3.7.1
pip              22.2
pipenv           2022.7.24
platformdirs     2.5.2
setuptools       58.1.0
six              1.16.0
virtualenv       20.15.1
virtualenv-clone 0.5.7

pipenv help

To create a virtual environment for python 2

# Create virual environment for python 2
$ python -m pipenv install --two

To create a virtual environment for python 3

# Create virual environment for python 3
$ python -m pipenv install --three

Creating a virtualenv for this project...
Pipfile: /Users/kenanhancer/Documents/projects/python_demo1/Pipfile
Using /Users/kenanhancer/.pyenv/versions/3.10.5/bin/python3 (3.10.5) to create virtualenv...
⠹ Creating virtual environment...created virtual environment CPython3.10.5.final.0-64 in 166ms
  creator CPython3Posix(dest=/Users/kenanhancer/.local/share/virtualenvs/python_demo1-hPpiqctY, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/kenanhancer/Library/Application Support/virtualenv)
    added seed packages: pip==22.1.2, setuptools==62.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment! 
Virtualenv location: /Users/kenanhancer/.local/share/virtualenvs/python_demo1-hPpiqctY
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (e4eef2)!
Installing dependencies from Pipfile.lock (e4eef2)...
  ?   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
$ tree -a

.
├── .python-version
├── Pipfile
└── Pipfile.lock

0 directories, 3 files

.python-version file is created by pyenv
Pipfile and Pipfile.lock are created by pipenv

3.10.3
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.10"
{
    "_meta": {
        "hash": {
            "sha256": "fedbd2ab7afd84cf16f128af0619749267b62277b4cb6989ef16d4bef6e4eef2"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.10"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {},
    "develop": {}
}
$ pipenv install requests

Installing requests...
Adding requests to Pipfile's [packages]...
✔ Installation Succeeded 
Installing dependencies from Pipfile.lock (d1a991)...
  ?   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

As shown in the following Piffle, requests package is added in [packages] section (in line 7)

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"

[dev-packages]

[requires]
python_version = "3.10"
$ pipenv graph

requests==2.28.1
  - certifi [required: >=2017.4.17, installed: 2022.6.15]
  - charset-normalizer [required: >=2,<3, installed: 2.1.0]
  - idna [required: >=2.5,<4, installed: 3.3]
  - urllib3 [required: >=1.21.1,<1.27, installed: 1.26.11]

To activate shell, type pipenv shell

$ kenanhancer@MacBook-Air python_demo1 % python -m pipenv shell

Launching subshell in virtual environment...
 . /Users/kenanhancer/.local/share/virtualenvs/python_demo1-hPpiqctY/bin/activate
kenanhancer@MacBook-Air python_demo1 %  . /Users/kenanhancer/.local/share/virtualenvs/python_demo1-hPpiqctY/bin/activate
$ (python_demo1) kenanhancer@MacBook-Air python_demo1 %

To exit shell, type exit

After activating pipenv shell, requests Python package is listed as well. So, we can list installed packages after activating pipenv shell or run pipenv graph without activating shell.

$ (python_demo1) kenanhancer@MacBook-Air python_demo1 % pip list

Package            Version
------------------ ---------
certifi            2022.6.15
charset-normalizer 2.1.0
idna               3.3
pip                22.1.2
requests           2.28.1
setuptools         63.2.0
urllib3            1.26.11
wheel              0.37.1
$ (python_demo1) kenanhancer@MacBook-Air python_demo1 % python -c "
import requests

#the required first parameter of the 'get' method is the 'url':
x = requests.get('https://w3schools.com/python/demopage.htm')

#print the response text (the content of the requested file):
print(x.text)
"

<!DOCTYPE html>
<html>
<body>

<h1>This is a Test Page</h1>

</body>
</html>

I install jq package to format JSON output.

$ (python_demo1) kenanhancer@MacBook-Air python_demo1 % python -c "
import requests

# Making a GET request
response = requests.get('https://api.github.com/users/kenanhancer')

# print content of request
print(response.text)" | jq

{
  "login": "kenanhancer",
  "id": 1851856,
  "node_id": "MDQ6VXNlcjE4NTE4NTY=",
  "avatar_url": "https://avatars.githubusercontent.com/u/1851856?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/kenanhancer",
  "html_url": "https://github.com/kenanhancer",
  "followers_url": "https://api.github.com/users/kenanhancer/followers",
  "following_url": "https://api.github.com/users/kenanhancer/following{/other_user}",
  "gists_url": "https://api.github.com/users/kenanhancer/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/kenanhancer/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/kenanhancer/subscriptions",
  "organizations_url": "https://api.github.com/users/kenanhancer/orgs",
  "repos_url": "https://api.github.com/users/kenanhancer/repos",
  "events_url": "https://api.github.com/users/kenanhancer/events{/privacy}",
  "received_events_url": "https://api.github.com/users/kenanhancer/received_events",
  "type": "User",
  "site_admin": false,
  "name": "kenan hancer",
  "company": "Lloyds Banking Group",
  "blog": "http://www.kenanhancer.com",
  "location": "United Kingdom",
  "email": null,
  "hireable": true,
  "bio": null,
  "twitter_username": null,
  "public_repos": 49,
  "public_gists": 38,
  "followers": 16,
  "following": 1,
  "created_at": "2012-06-14T21:04:20Z",
  "updated_at": "2022-07-24T08:21:49Z"
}

Creating virtual environment via venv and virtualenv

virtualenv (for Python 2) and venv (for Python 3) allow you to manage separate package installations for different projects.

To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.

Prerequisites

# If you don't have pip python package manager, run below command
$ python -m pip install pip

# If your pip python package manager needs upgrade, run below command
$ python -m pip install --upgrade pip
$ python -m pip install virtualenv

# If your virtualenv needs upgrade, run below command
$ python -m pip install --upgrade virtualenv

Creating project folder

$ mkdir python-demo
$ cd python-demo

Creating virtual environment for python 3.10.2

$ pyenv local 3.10.2
$ python -m venv .venv/3.10.2

Creating virtual environment for python 3.10.5

$ pyenv local 3.10.5
$ python -m venv .venv/3.10.5

Activating .venv/python 3.10.2 environment

$ source .venv/3.10.2/bin/activate
$ deactivate

Activating .venv/python 3.10.5 environment

$ source .venv/3.10.5/bin/activate
$ deactivate

Folder structure of created virtual environments

python-demo/
├── .python-version
└── .venv
    ├── 3.10.2
    │   ├── bin
    │   │   ├── Activate.ps1
    │   │   ├── activate
    │   │   ├── activate.csh
    │   │   ├── activate.fish
    │   │   ├── pip
    │   │   ├── pip3
    │   │   ├── pip3.10
    │   │   ├── python -> /Users/kenanhancer/.pyenv/versions/3.10.2/bin/python
    │   │   ├── python3 -> python
    │   │   └── python3.10 -> python
    │   ├── include
    │   ├── lib
    │   │   └── python3.10
    │   └── pyvenv.cfg
    └── 3.10.5
        ├── bin
        │   ├── Activate.ps1
        │   ├── activate
        │   ├── activate.csh
        │   ├── activate.fish
        │   ├── pip
        │   ├── pip3
        │   ├── pip3.10
        │   ├── python -> /Users/kenanhancer/.pyenv/versions/3.10.5/bin/python
        │   ├── python3 -> python
        │   └── python3.10 -> python
        ├── include
        ├── lib
        │   └── python3.10
        └── pyvenv.cfg

Leave a Reply