5

I did something very stupid. I was copying some self written packages to the python dist-packages folder, then decided to remove one of them again by just rewriting the cp command to rm. Now the dist-packages folder is gone. What do I do now? Can I download the normal contents of this folder from somewhere, or do I need to reinstall python completely. If so - is there something I need to be careful about?

The folder I removed is /usr/local/lib/python2.7 so not the one maintained by dpkg and friends.

tripleee
  • 175,061
  • 34
  • 275
  • 318
vicco
  • 1,049
  • 2
  • 14
  • 33
  • 1
    I think you might get a better answer here : http://superuser.com/about – ρss Jan 12 '16 at 10:19
  • Thanks, just posted it there, too. – vicco Jan 12 '16 at 10:21
  • 2
    Cross-site dupe: http://superuser.com/questions/1025129/accidentally-removed-dist-packages-folder-what-to-do-now but I think this is more suitable for https://unix.stackexchange.com/ or perhaps https://askubuntu.com/ if you don't mind getting wild guesses from preteens instead of qualified answers. – tripleee Jan 12 '16 at 11:56

3 Answers3

13

I guess you are using a debian based distribution (ubuntu or similar). If so, you have to reinstall all python packages. You should be able to get most of them "automatically" by calling:

 sudo dpkg --get-selections | grep -E "^python" | grep install | cut -f1 | xargs apt-get --reinstall -y install

Hope this helps. If you want to see which packages will be reinstalled, just call the first part of the piped commands:

sudo dpkg --get-selections | grep -E "^python"

Finally you should consider to use virtualenv or anaconda instead of installing or copying your own packages to dist-packages. If you don't want that, you could copy the packages to site-packages instead of dist-packages to seperate them from the distribution packages.

Niemerds
  • 932
  • 7
  • 12
  • Just tried it, the dist-packages folder is now there again, but it's empty. I executed it as root by doing `sudo su` first because it asked me whether I'm root and wouldn't execute it otherwise, but that should not matter, right? – vicco Jan 12 '16 at 11:06
  • "sudo su" in advance is fine If it works you should get messages on the console stating that apt-get reinstalls several packages. If not you probably get an error message? – Niemerds Jan 12 '16 at 11:29
  • Yes, it does install many packages, but the dist-packages folder is still empty. But wait - what dist-packages folder are you actually talking about? I deleted the one located at /usr/local/lib/python2.7, but there also is one at /usr/lib/python2.7. – vicco Jan 12 '16 at 11:34
  • Then you installed them with "pip" or "easy_install" as root. Right? Unfortunately I don't think a list of packages is maintained by pip outside of /usr/local/... by default. Thus, if you did not store a list manually somewhere, there is no way to reconstruct the deleted packages. – Niemerds Jan 12 '16 at 11:52
  • 1
    Yes, I installed some packages with pip as root, but I already got them back. That's not what I'm worried about anyway. I'm worried whether I caused any other damage by deleting the /usr/local/lib/python2.7 folder besides losing the stuff I installed with pip. – vicco Jan 12 '16 at 11:55
  • Don't think so. As you already figured out, the packages installed by the distribution goes into /usr/lib... rather than /usr/local/lib... – Niemerds Jan 12 '16 at 11:57
  • Oh, well then I was afraid for no reason. Thank you. – vicco Jan 12 '16 at 11:58
2

The directory you removed is controlled and maintained by pip. If you have a record of which packages you have installed with pip, you can force it to reinstall them again.

If not, too late to learn to make backups; but this doesn't have to be a one-shot attempt -- reinstall the ones you know are missing, then live with the fact that you'll never know if you get an error because you forgot to reinstall a module, or because something is wrong with your code. By and by, you will discover a few more missing packages which you failed to remember the first time; just reinstall those as well as you discover them.

As an aside, using virtualenv sounds like a superior solution for avoiding a situation where you need to muck with your system Python installation.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You can get a list of the packages installed by pip with pip list. Then you can run something like

pip list | tail -n+3 | cut -d' ' -f1 | xargs sudo pip install --force-reinstall

where

  • pip list gives a list of the installed packages (as registered in the database),
  • tail -n+3 skips the first two lines of output, which are just a heading,
  • cut -d' ' -f1 removes the package version from each line and
  • xargs sudo pip install --force-reinstall reinstalls each package anew.

The same thing happened to me, and this solution didn't entirely work out (some packages failed to install, for some reason) but it may work for you, or at least put you on the right path. (I realise this post is way to late, but this is for the people who run into this mishap in the future.)

Severo Raz
  • 174
  • 12