Zintis' pip cheat sheet

Home

1 Package Installer for Python, PIP

In addition to the standard library, there is a growing collection of several thousand components (from individual programs and modules to packages and entire application development frameworks), available from the Python Package Index. This Python Package Index or PyPi is available for browsing online. Here is PyPi You can, for example, search "cisco ACI" and get libraries already written for Cisco ACI.

This is where you will find what versions of a particular package are availalbe for install.

2 Some changes as of 3.8 on LINUX and on Mac OSX

On my CentOS8 systems the way to install and upgrade pip is:

  • pip3.8 list
  • python -m pip install --upgrade pip

I noticed that my python was correctly symlinked to /etc/alternatives, which in turn was correctly symlinked to /usr/local/bin/python3.8. Meaning that I actually ran:

  • python -m pip install --upgrade pip
  • as well as pip install --upgrade pip by itself. The second one seemed to notice that the pip upgrade was already done, so apparently they both do the same thing.

    My pip --version showed 20.2.3 after the upgrade in both commands.

Actually, to be clear, the above results were while I was in a venv which I had installed using python -m venv venv-ansible (and python was at 3.8 as explained above.

2.1 Mac OSX needs python3 -m pip install …

For my mac osx python, I had to use python3 -m pip install --upgrade pip because python actually ran python 2.7, where python3 ran python 3.9.1.

2.2 using curl to install pip

From docs.ansible.com :

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py --user
ansible@c8host /home/ansible[1011] $
pip3.8 list
Package    Version
---------- -------
pip        20.1.1
setuptools 47.1.0
WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.
You should consider upgrading via the '/usr/local/bin/python3.8 -m pip install --upgrade pip' command.
ansible@c8host /home/ansible[1012] $
python3.8 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
  Using cached pip-20.2.2-py2.py3-none-any.whl (1.5 MB)
Installing collected packages: pip
Successfully installed pip-20.2.2
WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.
You should consider upgrading via the '/usr/local/bin/python3.8 -m pip install --upgrade pip' command.
ansible@c8host /home/ansible[1013] $

2.3 pip

With Python installed, you also got pip, or Python Install Packages These "packages" are really modules. For example requests, json,

Common pip commands are:

pip help
pip help install
pip search meraki
pip install package
pip install meraki
pip install --upgrade package
pip install -U package
pip install -U pip     *** important to keep pip up to date (security)
pip install -r ~requirements.txt~   # which is a text file of python modules
pip uninstall package
pip freeze  # to list all the packages in ~requirements.txt~ format
pip list    # same thing but not in requirements format, but with options
pip list --outdated
pip list -o  (same thing)

2.4 which pip?

Could be one of the following:

  1. /usr/bin/env pip
  2. pip
  3. python3 -m pip
  4. pip3.8
  5. seems like the safest way to know which pip you are running is to use 3)

2.5 User written modules and site-packages directory.

2.5.1 Modules

Module is a python program snippet (no shebang line)

  • they can be user created
  • they can be part of the standard library that comes with your python version
  • they can be added from pypi.org (but typically as part of a package)

2.5.2 Packages

Packages are sets of python modules stored in a directory. More accurately, a package is a namespace for a collection of modules. Because The namespace part is important because by itself package doesn’t provide any functionality ==> it only gives you a way to group a bunch of your modules.

  • they are added to your environemnt using pip
  • pip downloads and installs them from pypi.org
  • they are store locally in <python_root_folder>/lib/pythonxxx/site-packages
  • every virtual environemnt will have a site-pages folder For example /bin/venv-xpress/lib/python3.6/site-packages
  • I also see that there is the ~/.local/lib/python3.6/site-packages folder but that does NOT seem to change whether I am in a pyenv or not. see below:

2.5.3 Asking python where are the site-packages?

/Users/zintis/bin[28] % source venv-xpress/bin/activate.csh
(venv-xpress) /Users/zintis/bin[30] % python -m site --user-site
(venv-xpress) /Users/zintis/bin[30] % python -m site --user-site
//Users/zintis/.local/lib/python3.6/site-packages
(venv-xpress) /Users/zintis/bin[31] % deactivate


/Users/zintis/bin[31] % source venv-cisco/bin/activate.csh
(venv-cisco) /Users/zintis/bin[8] % python -m site --user-site
/Users/zintis/.local/lib/python3.7/site-packages
(venv-cisco) /Users/zintis/bin[9] %

2.5.4 if site-packages does not change with venv, does pip list change with venv?

As of Jan 2020 python3 -m site --user-site showed that my packages are in:

  • /Users/zintis/Library/Python/3.9/lib/python/site-packages

But in a venv like (venv-webdev) my packages are STILL in:

  • /Users/zintis/Library/Python/3.9/lib/python/site-packages

But in the (venv-webdev) at least which python gave me:

  • /Users/zintis/bin/python/venv-webdev/bin/python
  • python –version resulted in Python 3.9.1 which is also correct

Most importantly, the python -m pip list in my (venv-webdev) showed a much longer list of packages than my global python3 -m pip list command showed.

And to remind myself, I used python3 -m venv venv-webdev to create that virtual environment. All is good.

2.6 pip freeze

I use pip freeze to list them. As of May 2nd, 2019 my pip freeze is: Last login: Wed May 1 21:27:04 on ttys000 /Users/zintis[2] % pip freeze

pip freeze pip freeze (new mbp)
pip 19.0.1??? check pip 19.2.2
appdirs==1.4.3  
asn1crypto==0.24.0 asn1crypto==0.24.0
attrs==19.1.0  
autopep8==1.34.4 autopep8==1.4.4
bcrypt==3.1.6 backcall==0.1.0
black==19.3b0  
  bcrypt==3.1.7
certifi==2018.11.29 certifi==2019.6.16
cffi==1.12.2 cffi==1.12.3
chardet==3.0.4 chardet==3.0.4
ciscosparkapi==0.9.2 ciscosparkapi==0.10.1
Click==7.0  
cryptography==2.6.1 cryptography==2.7
  decorator 4.4.0
dnac==1.2.10.1 dnac==1.2.10.3
elpy==1.999 elpy==1.999 **use lsp-mode
entrypoints==0.3  
flake8==3.7.7 flake8==3.7.8
future==0.17.1  
  ipython==7.7.0
  ipython-genutils==0.2.0
idna==2.5  
jedi==0.13.3 jedi==0.15.1
lxml==4.3.3  
mccabe==0.6.1 mccabe==0.6.1
meraki==0.34  
ncclient==0.5.3  
netmiko==1.4.2  
paramiko==2.4.2  
parso==0.4.0 parso==0.5.1
prettytable==0.7.2  
pyang==1.7.4  
pyasn1==0.4.5  
p*ycodestyle==2.5.0*  
pycparser==2.19  
pyflakes==2.1.1  
PyNaCl==1.3.0  
PyYAML==5.1  
requests==2.18.2  
requests-toolbelt==0.9.1  
rope==0.14.0  
scp==0.13.2  
six==1.12.0  
toml==0.10.0  
urllib3==1.21.1  
virtualenv==16.4.3 virtualenv==16.7.6
xmltodict==0.11.0  
yapf==0.27.0  
  setuptools 41.1.0
  wheel 0.33.4

2.7 pip freeze (venv-cisco)

pip freeze pip freeze (new mbp)
pip 19.0.1??? check pip 19.3.1
appdirs==1.4.3  
asn1crypto==0.24.0 asn1crypto==1.2.0
attrs==19.1.0  
autopep8==1.34.4 autopep8==1.4.4
bcrypt==3.1.6 backcall==0.1.0
black==19.3b0  
  bcrypt==3.1.7
certifi==2018.11.29 certifi==2019.6.16
cffi==1.12.2 cffi==1.12.3
chardet==3.0.4 chardet==3.0.4
ciscosparkapi==0.9.2 ciscosparkapi==0.10.1
Click==7.0  
cryptography==2.6.1 cryptography==2.7
  decorator 4.4.0
dnac==1.2.10.1 dnac==1.2.10.3
elpy==1.999 elpy==1.999
entrypoints==0.3  
  future==0.17.1
flake8==3.7.7  
future==0.17.1  
  ipython==7.11.0
  ipython-genutils==0.2.0
idna==2.5 idna==2.8
jedi==0.13.3 jedi==0.15.1
lxml==4.3.3 lxml==4.4.1
mccabe==0.6.1  
meraki==0.34 meraki==0.34
ncclient==0.5.3 ncclient==0.6.6
netmiko==1.4.2 netmiko==2.4.2
paramiko==2.4.2 paramiko==2.7.1
parso==0.4.0 parso==0.5.1
  pexpect==4.7.0
prettytable==0.7.2  
  pickleshare==0.7.5
  prompt-toolkit==2.0.9
  ptyprocess==0.6.0
  pycodestayle==2.5.0
pyang==1.7.4 pyang==2.1
  pycparser==2.19
pyasn1==0.4.5  
p*ycodestyle==2.5.0*  
   
pycparser==2.19 pycparser==2.19
pyflakes==2.1.1  
PyNaCl==1.3.0  
  Pygments==2.4.2
  PyNaCl==1.3.0
  pyserial==3.4
PyYAML==5.1  
requests==2.18.2 requests==2.22.0
requests-toolbelt==0.9.1 requests-toolbelt==0.9.1
rope==0.14.0  
scp==0.13.2 scp==0.13.2
six==1.12.0 six==1.12.0
toml==0.10.0  
  textfsm==1.1.0
urllib3==1.21.1  
virtualenv==16.4.3  
  traitlets==4.3.2
xmltodict==0.11.0  
yapf==0.27.0  
  urllib3==1.25.7
  wcwidth==0.1.7
   

2.8 brew unlink to fix pip that is missing

If you have installed python, but which pip comes up empty, you may have to brew unlink python followed by brew link python to fix it.

.
/Users/zintis/eg[70] % brew unlink python
Unlinking /usr/local/Cellar/python/3.7.4... 24 symlinks removed
/Users/zintis/eg[71] % brew link python
Linking /usr/local/Cellar/python/3.7.4... 24 symlinks created
/Users/zintis/eg[72] % which pip
pip: Command not found.
/Users/zintis/eg[73] % 

2.9 fix for pip

The above unlink did not work but all I had to do was reboot my macbook. Then I got:

/Users/zintis[1] % which pip
/Users/zintis/.pyenv/shims/pip
/Users/zintis/eg[73] % 

Usually, the requirments.txt, if present, was included in the downloaded python module if that module had any prerequisites. This way the module can ensure that all prerequisites are also installed.

See the section on virtual environments to see how to create the requirements.txt for your own projects. (I can use org-mode to add a link to this section within this same file, but I don't quite know how. .. will fix this at a later date.)

2.10 How to install modules from other sources (directories).

When the modules you want pip to install are not in the usual Pypi site, you must download them to a folder and then … what? Check out stackoverflow.com on this topic.

That was as good reference site, but in the end, I had to run the setup.py script in the root directory of the downloaded cobra folder, like so:

2.11 Fixing permission errors in pips. –user option

Here is an example of a pip install with errors, and subsequent corrections:

= /Users/zintis/bin[11] % pip install –upgrade virtualenv Collecting virtualenv Downloading https://files.pythonhosted.org/packages/4f/ba/6f9315180501d5ac3e707f19fcb1764c26cc6a9a31af05778f7c2383eadb/virtualenv-16.5.0-py2.py3-none-any.whl (2.0MB)

████████████████████████████████ 2.0MB 474kB/s

Installing collected packages: virtualenv Found existing installation: virtualenv 16.4.3 Uninstalling virtualenv-16.4.3: ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: 'zip-safe' Consider using the `–user` option or check the permissions.

/Users/zintis/bin[12] % pip install –upgrade –user virtualenv Collecting virtualenv Using cached https://files.pythonhosted.org/packages/4f/ba/6f9315180501d5ac3e707f19fcb1764c26cc6a9a31af05778f7c2383eadb/virtualenv-16.5.0-py2.py3-none-any.whl Installing collected packages: virtualenv Successfully installed virtualenv-16.5.0

2.12 Additional info and example

Pip install documention on pip.pypa.io

This doc.python-guide.org link has some good explanation of additional info. Worth the read. specifically on pipenv, a virtual environment package manager for python modules.

  • pip in virtualenv (python2 or python3)
  • venv (python3)

2.12.1 review setup.py as a means of specifying which modules are needed

and which should be kept up to date automatically. It turns out that there are multiple package managers for python, no just pip. I think I will stick to pip but so that I am not mislead or confused and unknowingly start using two managers and get all messed up here is a list of package managers:

  • distutils : part of the standard library
  • distribute : discountinued
  • distribute2 : discountinued
  • setuptools :
  • pip : Python Install Package
  • eggs : introduced 2004 and was never official PEP
  • wheels : newest official package manager available on pythonwheels introduced in 2012

    Some installs actually try to use the newer setuptools and still fall back to the older distribute tools

    try:
        from setuptools import setup
    except:
        from distutils.core import setup
    

Recommendation is to use pip. Although python setup.py install and pip install <Package-name> does the same thing, pip gives you these advantages:

  • pip will automatically download all dependencies for a package for you
  • pip keeps track of various metadata that lets you easily uninstall and update packages with a single command pip unistall <package>
  • pip lets you easily upgrade pip install --upgrade <package>
  • pip automatically downloads the files for you (setup insists you have them pre-downloaded)
  • pip will let you install wheels

3 Pipenv

Newest (circa late 2018) is to use pipenv

This is pip combined with virtual environments. Before pipenv I would use

  • pip for package management, and
  • venv for virtual environments (or virtual env)

3.1 Installing pipenv

First pip freeze gave me this list, with which I highlight what is added when I install pipenv:

new already satisfied

- appdirs==1.4.4

  • appnope==0.1.2
  • backcall==0.2.0
  • beautifulsoup4==4.9.3

- certifi==2020.11.8

  • chardet==3.0.4
  • cycler==0.10.0
  • decorator==4.4.2

- distlib==0.3.1 - filelock==3.0.12

  • flake8==3.8.4
  • idna==2.10
  • ipython==7.19.0
  • ipython-genutils==0.2.0
  • jedi==0.17.2
  • kiwisolver==1.3.1
  • matplotlib==3.3.3
  • mccabe==0.6.1
  • numpy==1.19.4
  • pandas==1.1.4
  • parso==0.7.1
  • passlib==1.7.4
  • pexpect==4.8.0
  • pickleshare==0.7.5
  • Pillow==8.0.1

- pipenv==2020.11.15

  • prompt-toolkit==3.0.8
  • ptyprocess==0.6.0
  • pycodestyle==2.6.0
  • pyflakes==2.2.0
  • Pygments==2.7.2
  • pyparsing==2.4.7
  • python-dateutil==2.8.1
  • pytz==2020.4
  • requests==2.25.0
  • scipy==1.5.4

- six==1.15.0

  • soupsieve==2.0.1
  • traitlets==5.0.5
  • urllib3==1.26.2

- virtualenv==20.2.2 - virtualenv-clone==0.5.4

  • wcwidth==0.2.5

4 Installing my first pipenv virtual environment using pipenv

Since I wanted to use my latest (at that time) python, which was 3.9, but I only had 3.9 which was itself in a virtual enviroment I used a hack (my guess) which was to find out where my 3.9 python was located (while in my 3.9 virtaul enviroment). Then deactivate out of that virutual environment. But then use a directed python3 command to and pipenv from that directory to create a new pipenv virtual environment that would use 3.9

4.1 install pipenv using pthon3.9

This is what I did: /bin/python/venv-webdev/bin/python -m pipenv install requests

/Users/zintis/bin/python/mynewpythproject[532]:
$ ~/bin/python/venv-webdev/bin/python -m pipenv install  requests
Creating a virtualenv for this project...
Pipfile: /Users/zintis/bin/python/mynewpythproject/Pipfile
Using /usr/local/bin/python3 (3.9.0) to create virtualenv...
⠋ Creating virtual environment...created virtual environment CPython3.9.0.final.0-64 in 726ms
  creator CPython3Posix(dest=/Users/zintis/.local/share/virtualenvs/mynewpythproject-F9bljqZV, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/zintis/Library/Application Support/virtualenv)
    added seed packages: pip==20.3.1, setuptools==51.0.0, wheel==0.36.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment! 
Virtualenv location: /Users/zintis/.local/share/virtualenvs/mynewpythproject-F9bljqZV
Creating a Pipfile for this project...
Installing requests...
Adding requests to Pipfile's [packages]...
✔ Installation Succeeded 
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success! 
Updated Pipfile.lock (fe5a22)!
Installing dependencies from Pipfile.lock (fe5a22)...
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
/Users/zintis/bin/python/mynewpythproject[533]:
$ 

As you can see, it looks like 3.9 was used, but directly, through /usr/local/bin/python3 which I checked afterward was a symlink to /usr/local/Cellar/python@3.9/3.0.01/bin/python3

What the /bin/python/venv-webdev/bin/python -m pipenv install requests command created:

/Users/zintis/bin/python/mynewpythproject[535]:
$ tree
.
├── Pipfile
└── Pipfile.lock

0 directories, 2 files
/Users/zintis/bin/python/mynewpythproject[536]:

4.2 But where is the virtual environemnt?

If you read the output above carefully, you see that it is in: Users/zintis.local/share/virtualenvs/mynewpythproject-F9bljqZV

You can cd to this directory and manually activate the virtual env, but you do not have to. It is much easier to cd into the project directory then run pipenv shell or pip run myscript.py if you want to just run this one script in this virtual environment.

4.3 activating virtual environment

To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run.

4.4 deactivating virtual environment

Do NOT use deactivate. Rather remember that pipenv shell command has put you into a subshell, and that subshell should be exited with the command exit.

If you do run deactivate, it will change your prompt just like the oldschool way, but it will NOT exit your subshell. So remember, just use plain old exit

5 Pipfile

You can see from above, that pipenv uses a new file, called Pipfile which is in essence the same thing that a requirements.txt file is. i.e. a list of packages in this current virtual environment.

My pipfile is in /bin/python/mynewpythonproject/Pipfile and the virtual env that was created is in ~/.local/share/virtualenvs/mynewproject-F9bljqZVdddddddddddddddddddddddddddddddddd

5.1 First time creating a virtual environment

If this is the 1st time creating a new virtual environment, you will create these two files, automatically.

  1. Pipfile
  2. pipfile.lock

5.2 What versions of packages will be installed?

As mentioned the Pipfile is just like a requirements.txt, but in a TML file very similar to a YAML file, but without the need for strict indentation.

Pipfile will let you specify a specific version of a package, or if you say "*" then pipenv will keep this package to the latest version.

If you need to keep an exact (even if slightly out of date) version, tnen you can see the pipfile.lock.

pipfile.lock

Will explicitly specify what version of each package is needed. It does that by comparing the SHA hash of the package. This pipfile.lock should NOT be edited directly. The commands pipenv lock command will "lock" this versions in place. You can then expllicitly update the lock (which updates the versions of each of the plackages according to what is "current" in the pipenv as of that moment.

5.2.1 specifying the version you want

  • python -m pip install --upgrade pandas==1.3.5

6 sys.executable

With all these different version of python floating around, it is easy to get confused which python is actually running. A very good command to learn is sys.executable and typically run this in an interactive python shell.

First you have to import sys and then run sys.executable.

It will tell you the final (after all symlinks have been traced back) python executable.

7 which python

sometimes you get the wrong python, even after an install of a newer python.

These techniques can be tried:

  1. Run python3 and not python
  2. Run which python and follow through by long listings to track symlinks.
  3. type python3 very similar to which python3
  4. check path by echo $PATH, or set | grep -i path
  5. The $PATH is searched in order, so you may be hitting an executable that appears earlier in the path where you were not expecting it
  6. import sys; sys.executable (run this in interactive python
  7. check aliases as you may have had alias python=python3 or something similar
  8. pip show packagename

7.1 Home