Archive
pip install –user
Problem
When we install something with pip, usually we do a “sudo pip install pkg_name“. However, there are some problems with this approach. First, you need root privileges. Second, it installs the package globally, which can cause conflicts in the system. Is there a way to install something with pip locally?
Solution
The good news is that you can install a package with pip locally too. Under Linux the destination folder by default is ~/.local . Add the following line to the end of your ~/.bashrc :
export PATH=~/.local/bin:$PATH
Then install the package locally. For instance, let’s install pipenv:
$ pip install pipenv --user
Open a new terminal (thus ~/.bashrc is read), and launch pipenv. It should be available. Let’s check where it is:
$ which pipenv /home/jabba/.local/bin/pipenv
update all packages with pip in your virtual environment
Problem
You want to update all installed packages in your virtual environment.
Solution
$ pip install pip-review $ pip-review --local --interactive
Tip from here.
python setup.py uninstall
Problem
Sometimes you need to install a software with “python setup.py install“. However, if you want to get rid of it, there is no “python setup.py uninstall” or “python setup.py remove”. Stupid, but true.
Solution
I found the solution here. In short:
# install: $ (sudo) python setup.py install # uninstall: $ pip freeze | grep PATTERN_BASED_ON_PACKAGE_NAME # then remove with pip: $ (sudo) pip uninstall PACKAGE_NAME
pip-tools
“A set of two command line tools (pip-review + pip-dump) to help you keep your pip-based packages fresh, even when you’ve pinned them.
pip-review checks PyPI and reports available updates. It uses the list of currently installed packages to check for updates, it does not use any requirements.txt.
pip-dump dumps the exact versions of installed packages in your active environment to your requirements.txt file.”
I haven’t used it yet, so this post a reminder for me. I think I will need it soon.
PyPy is awesome
PyPy is a fast, compliant alternative implementation of the Python language (2.7.3 and 3.2.3).
Installation
Visit the download page and get the 32-bit or 64-bit archive, depending on your architecture. I extracted it in the /opt directory and put a symbolic link on it:
jabba@jabba-uplink:/opt$ ls -al | grep pypy lrwxrwxrwx 1 jabba jabba 8 Aug 15 12:32 pypy -> pypy-2.1 drwxr-xr-x 7 jabba jabba 4096 Jul 31 12:59 pypy-2.1
If you install a newer version, just update the symbolic link.
Put another symbolic link to /usr/bin called pypy that points to /opt/pypy/bin/pypy:
root@jabba-uplink:~# ls -al /usr/bin/pypy lrwxrwxrwx 1 root root 18 Aug 15 16:39 /usr/bin/pypy -> /opt/pypy/bin/pypy
Try it:
jabba@jabba-uplink:~$ pypy Python 2.7.3 (480845e6b1dd, Jul 31 2013, 09:57:07) [PyPy 2.1.0 with GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>>
Install pip for pypy
We also want to use 3rd party libraries with pypy thus we need pip.
$ wget http://python-distribute.org/distribute_setup.py $ wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py $ pypy distribute_setup.py $ pypy get-pip.py
Add the following alias to your ~/.bashrc:
alias pip_pypy='/opt/pypy/bin/pip'
Open a new terminal and you are ready to install 3rd party libraries. Example:
$ pip_pypy install futures # import concurrent.futures
update all pip packages
Problem
You want to update all the pip-installed Python packages on your system.
Solution
At http://stackoverflow.com/questions/2720014/ there is a very nice little script for this task:
import pip
from subprocess import call
for dist in pip.get_installed_distributions():
call("pip install --upgrade " + dist.project_name, shell=True)
I modified it a bit to update packages in ascending order by name in an ignore-case way:
#!/usr/bin/env python
import os
import pip
dists = []
for dist in pip.get_installed_distributions():
dists.append(dist.project_name)
for dist_name in sorted(dists, key=lambda s: s.lower()):
cmd = "sudo pip install {0} -U".format(dist_name)
print '#', cmd
#os.system(cmd)
I want to update the packages globally, that’s why the “sudo“. If you run it, it will just print the commands to the stdout (dry run). If everything is OK, uncomment the last line.
Update (20140121)
I made a github project of it: https://github.com/jabbalaci/update-pip-packages.
Install a package with pip from a github repository
Problem
You want to install a package from a GitHub repository via pip.
Solution
Let’s take an example. Say you want to install requests whose github address is https://github.com/kennethreitz/requests.
With pip you can istall it like this:
sudo pip install -U https://github.com/kennethreitz/requests/zipball/master
Install a package via pip even if PyPI is down
Upload your first project to PyPI
Today I uploaded my jabbapylib project to PyPI, available here.
For preparing my first public pypi project, I followed this tutorial. For understanding how to package a Python library, read Mark Pilgrim’s corresponding book chapter.
I only want to add the following remarks:
- In
setup.py, I import “fromsetuptoolsimport setup, find_packages“. Noticesetuptoolsinstead ofdistutils.core. - In the prepared package I wanted to include the library
jabbapylib/recursively since all the source codes are there. I could do that with this line: “packages = find_packages(exclude=['demos', 'dist', 'tests'])“, i.e. include all subdirectories with the exception of the ones in the list. What remains in my case is the “jabbapylib” folder. - My library has some dependencies, they are specified here: “
install_requires=['html5lib', 'psutil', 'pytest']“. When jabbapylib is installed with pip, pip will install these packages too. - You can create your own
MANIFEST.infile to specify what to include and what to exclude. However, if you want to add the directory that contains all the sources, do as indicated in step (2). - For checking, packaging and uploading I made some simple scripts.
The Python Ecosystem
Here is an excellent introductory article to the Python ecosystem including packages, pip, virtualenv (and virtualenvwrapper), and some other important tools (e.g. fabric). I wish I had read it a long time ago.

You must be logged in to post a comment.