Gangmax Blog

Remove Dependencies with Package Using Python pip

When running “pip uninstall ppp” to remove a Python pip package, unlike “apt-get remove uuu”, it seems it removes the package only without its useless depending packages, which will leave unused files there. How to improve it?

Here is a solution: it introduces “pip-autoremove“ which is also a Python pip package. When you want to remove a pip package, run the following command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 1. Install "pip-autoremove".
~> pip install pip-autoremove
Collecting pip-autoremove
Using cached pip_autoremove-0.9.0-py2.py3-none-any.whl
Installing collected packages: pip-autoremove
Successfully installed pip-autoremove-0.9.0
# 2. Use "pip-autoremove" to remove a pip package.
~/lang/python/douban.fm> pip-autoremove douban.fm
douban.fm 0.4.15 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
termcolor 1.1.0 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
requests 2.18.4 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
urllib3 1.22 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
idna 2.6 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
chardet 3.0.4 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
certifi 2017.7.27.1 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
pycookiecheat 0.4.3 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
keyring 10.3.2 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
SecretStorage 2.3.1 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
cryptography 2.0.3 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
cffi 1.11.2 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
pycparser 2.18 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
ipaddress 1.0.18 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
asn1crypto 0.23.0 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
enum34 1.1.6 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
pycrypto 2.6.1 (/home/user/.pyenv/versions/2.7.13/lib/python2.7/site-packages)
Uninstall (y/N)?y
Uninstalling termcolor-1.1.0:
Successfully uninstalled termcolor-1.1.0
Uninstalling ipaddress-1.0.18:
Successfully uninstalled ipaddress-1.0.18
Uninstalling asn1crypto-0.23.0:
Successfully uninstalled asn1crypto-0.23.0
Uninstalling douban.fm-0.4.15:
Successfully uninstalled douban.fm-0.4.15
Uninstalling pycookiecheat-0.4.3:
Successfully uninstalled pycookiecheat-0.4.3
Uninstalling pycparser-2.18:
Successfully uninstalled pycparser-2.18
Uninstalling chardet-3.0.4:
Successfully uninstalled chardet-3.0.4
Uninstalling keyring-10.3.2:
Successfully uninstalled keyring-10.3.2
Uninstalling enum34-1.1.6:
Successfully uninstalled enum34-1.1.6
Uninstalling cryptography-2.0.3:
Successfully uninstalled cryptography-2.0.3
Uninstalling cffi-1.11.2:
Successfully uninstalled cffi-1.11.2
Uninstalling idna-2.6:
Successfully uninstalled idna-2.6
Uninstalling certifi-2017.7.27.1:
Successfully uninstalled certifi-2017.7.27.1
Uninstalling urllib3-1.22:
Successfully uninstalled urllib3-1.22
Uninstalling SecretStorage-2.3.1:
Successfully uninstalled SecretStorage-2.3.1
Uninstalling pycrypto-2.6.1:
Successfully uninstalled pycrypto-2.6.1
Uninstalling requests-2.18.4:
Successfully uninstalled requests-2.18.4

However, the above logging output is gotten under “Python 2.7.13”. When running under “Python 3.6.1”, I got the following error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
~> pip-autoremove douban.fm
Traceback (most recent call last):
File "/home/user/.pyenv/versions/3.6.1/bin/pip-autoremove", line 11, in <module>
sys.exit(main())
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 109, in main
autoremove(args, yes=opts.yes)
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 21, in autoremove
dead = list_dead(names)
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 29, in list_dead
graph = get_graph()
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 92, in get_graph
for req in requires(dist):
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 557, in get_distribution
dist = get_provider(dist)
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 431, in get_provider
return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 719, in find
raise VersionConflict(dist, req)
pkg_resources.VersionConflict: (six 1.10.0 (/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages), Requirement.parse('six==1.8.0'))

# After I change package "six" version from "1.10.0" to "1.8.0", I got the following error:
~> pip-autoremove douban.fm
Traceback (most recent call last):
File "/home/user/.pyenv/versions/3.6.1/bin/pip-autoremove", line 11, in <module>
sys.exit(main())
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 109, in main
autoremove(args, yes=opts.yes)
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 21, in autoremove
dead = list_dead(names)
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 29, in list_dead
graph = get_graph()
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pip_autoremove.py", line 92, in get_graph
for req in requires(dist):
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 557, in get_distribution
dist = get_provider(dist)
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 431, in get_provider
return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
File "/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 719, in find
raise VersionConflict(dist, req)
pkg_resources.VersionConflict: (six 1.8.0 (/home/user/.pyenv/versions/3.6.1/lib/python3.6/site-packages), Requirement.parse('six>=1.9.0'))

It seems someone else met this issue before but there is no solution for this situation yet.

Comments