简体   繁体   中英

Physically uninstalled Python 3.4 to make pip3 install “requests” package to Python 3.5, how do I fix the mess that ensued?

I am using Ubuntu 14.04

I wanted to install package "requests" to use in python 3.5, so I installed it using pip3. I could see it in /usr/lib/python3.4, but while trying to actually execute scripts with Python 3.5 I always got "ImportError: No module named 'requests'"

OK, so I figured, perhaps that's because the package is not in python3.5 but in python3.4. Therefore, I tried to uninstall and install i again, but it just kept popping up where I didn't want it (not to mention, when I run apt-get remove pip3-requests, it actually removed pip3 for me as well lol). Therefore, I tried physically removing python3.4 from usr/lib and usr/local/lib in order to try and see if maybe pip3 was confused and installed packages in wrong directories.

I'm afraid it was not a good idea... when I now run eg
sudo pip3 install reqests

I get the following error:
Could not find platform independent libraries <prefix> Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings'

Is there any way to fix this now? And to actually use requests package?

When I use

sudo apt-get install python3-pip

It works and starts unpacking etc. but then I get a long error that starts with:

Setting up python3.4 (3.4.3-1ubuntu1~14.04.3)
Could not find platform independent libraries <prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted
dpkg: error processing package python3.4 (--configure):
subprocess installed post-installation script returned error exit status 134
dpkg: dependency problems prevent configuration of python3:

(...) and ends with

python3 depends on python3.4 (>= 3.4.0-0~); however:
 Package python3.4 is not configured yet.
dpkg: error processing package python3-wheel (--configure):
dependency problems - leaving unconfigured
E: Sub-process /usr/bin/dpkg returned an error code (1)

First of all, it is a very bad idea to remove your system Python 3 in Ubuntu (which 3.4 is in recent subrevisions of Trusty LTS) . That is because it is a vital part of the system . If you run the command apt-cache rdepends python3 , you'd see that packages such as ubuntu-minimal , ubuntu-release-upgrader-core , lsb-release , lsb-core , ubuntu-core-libs and so on, all depend on Ubuntu's version of Python 3 being installed (and this is the python3.4 in Ubuntu 14.04.4). If you force-remove python 3.4 by hand, you've ruined your system.

It might very well be that you now have to reinstall the whole operating system, unless you manage to reinstall all the system .deb packages that put data in /usr/lib/python3.4 .

And especially so if you do it with force. It can make your system even unbootable, so do not reboot that computer before you've successfully reinstalled Python 3... actually I am not sure how to do it safely since it seems you've forcefully removed all system dependencies from the /usr/lib)


You should try to reinstall python3.4

sudo apt-get install --reinstall python3.4

But now the bigger problem is that you've still missing all sorts of dependencies for your system programs.

Do note that pip also should be available as a module . Thus to ensure that you install for Python 3.5, you can do

sudo python3.5 -mpip install requests

The pip3 is a wrapper for a pip that installs to the system Python 3 version (3.4 in your case).

Ubuntu 14.04LTS uses the trusty package list . That repository comes with Python 3.4.0-0ubuntu2 . So the pip contained in python3-pip belongs to that version: 3.4.

As such, when using Python 3.5, packages installed using Python 3.4 and that version's pip will not be available.

I don't know how you installed Python 3.5 on your system, but you should use that way to install pip for that version as well. If you compiled it from source yourself, you should see the install instructions for pip on how to get it installed for Python 3.5.

  1. Open a text-only virtual console by using the keyboard shortcut Ctrl + Alt + F3 .

  2. At the login: prompt type your username and press Enter .

  3. At the Password: prompt type your user password and press Enter .

  4. Reinstall the default Python 3 version by running the following command:

     sudo apt install python3-all
  5. Switch out of the virtual console and return to your desktop environment by pressing the keyboard shortcut Ctrl + Alt + F7 . In Ubuntu 17.10 and later press the keyboard shortcut Ctrl + Alt + F2 to exit from the virtual console.


After you have installed the default Python 3 version, you need to get back your default Ubuntu desktop system. In order to avoid messing something up, do it in the following order:

  1. First install the terminal from the console using the command: sudo apt install gnome-terminal . If you can't install gnome-terminal at all, skip this step and go to step 2.

  2. Return to your desktop and open the terminal using the keyboard shortcut Ctrl + Alt + T . From the terminal install the Ubuntu Software Center using the command:

     sudo apt install software-center

    In Ubuntu 16.04 and later run this command instead to reinstall the default Software application:

     sudo apt install gnome-software

    If you still can't open the terminal, run the same command from the console instead. If you can't install the default software application at all, skip this step and go to step 3.

  3. Open the terminal and try to open the Ubuntu Software Center from the terminal by running the appropriate command, either: software-center or gnome-software . From the Ubuntu Software Center install the Ubuntu desktop system or else open the terminal (or the console) and install the Ubuntu desktop system by running the command: sudo apt install ubuntu-desktop .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM