简体   繁体   中英

Remove 1 version of Python in Ubuntu

So apparently I have 2 Pythons (same version) installed in different folders...one is in /usr/bin/ and the other one is in /usr/local/bin but the one the shell uses when I type in python is the one in /usr/local/bin . I'd like to use the /usr/bin/ version because is the one that works with many imports I've been dealing with such as numpy , matplotlib and Tkinter .

I've tried using pyenv but with this I cannot run Tkinter because Tkinter is installed only for the /usr/bin/ version.

  • Is there a safe way I can delete/uninstall one of those versions without breaking my whole Ubuntu?

  • Is there a way to tell the shell to use the /usr/bin/ version of Python?

  • Is there a way I can install python-tk for any envpy version? Something like sudo apt-get install python-tk in-desired-folders or similar?

Answer for any of those 3 questions would solve my problem, I think.

Thank you all in advice!

If these two Python installations are identical (same Python version), there is no reason you can't use the Python packages installed for one version with the other. You'd just have to adjust your PYTHONPATH :

export PYTHONPATH=/usr/lib/python2.7/site-packages

or variants thereof, depending where exactly the standard (system) Python has installed its packages.

You can find the latter by starting that Python explicitly, and looking at sys.path . On my Ubuntu system, for example:

> /usr/bin/python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/evert/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

and my PYTHONPATH should be set to

export PYTHONPATH=/usr/lib/python2.7/dist-packages

instead.


Alternatively, and perhaps even easier, just create an alias to the Python you want to use (just don't name the alias python ; it will sow lots of confusion):

alias py2=/usr/bin/python

and use that instead.

In either case, no need to remove anything in /usr/local/ (or even putting /usr/bin/ at the front of your PATH ); you just move the /usr/local directory/Python out of your way.

Without more knowledge of how you ended up where you are, there are no guarantees for "safe".

If you used pip or similar to install Python software then it will have installed for whichever Python version your system is preferring, which means the one in /usr/local/bin .

The system-supplied version in /usr/bin should probably not be touched or removed. If you installed components using apt-get install (or its cousins, aptitude , Synaptic, etc) then they will have been installed for the system Python.

Fundamentally, Apt and pip are competing to manage your Python code, and mixing them will end up in a situation where there is no simple way to reconcile. If you only used one or the other, then it should be easy to manage the code you already have installed, and modify it if you need to.

I second the suggestion to use virtualenv , perhaps as a first step towards a single install; but I expect you will find that you can actually live comfortably with multiple Python versions then. With virtualenv, you are not dependent on what is installed system-wide in the first place.

Typically, you would set up (at least) one virtual environment for each Python project you work on, and populate it with only the libraries which that particular project needs. So for example, to set up a virtualenv for using the system Python and use the libraries you specified, you'd do something like

vnix$ virtualenv -p /usr/bin/python myproject

vnix$ ./myproject/bin/activate

(myproject) vnix$ pip install numpy matplotlib # Tkinter is part of standard Python install

(myproject) vnix$ emacs myproject.py &  # hack away

The virtual environment contains a copy of your bare Python install (though that is hardly "bare", with all the batteries included) and anything you pip install while the virtualenv is active will only be installed inside the virtual environment. That way, you can nicely isolate dependencies, and work on software even with conflicting requirements simply by switching from one virtual environment to another.

Thus, if you like, you can create a second virtual environment with the locally installed Python and install Tkinter inside that virtual environment simply by running pip install when the environment is activated.

(myproject) vnix$ deactivate

vnix$ cd ..

vnix$ virtualenv myproject-local

vnix$ . ./myproject-local/bin/activate

(myproject-local) vnix$ pip install numpy matplotlib Tkinter

(Not sure how you could end up without Tkinter in your Python install so also not sure how you would install it there. pip install Tkinter does not seem to work at least where I am, so additional hacks are probably necessary. Install tkinter for Python seems relevant. Rebuilding and reinstalling Python in /usr/local , this time with Tkinter, seems like a plausible, albeit somewhat unattractive solution.)

As an aside, you probably want your project in version control; there is no need for the virtual environments to end up there, and in fact, virtualenv works nicely completely outside of your source tree if you prefer that. As an extreme example, you could create your virtual environments in /tmp and have them removed at reboot; the location of the virtualenv tree on disk is unimportant. (Maybe you want requirements.txt in version control so that you can easily recreate the virtual environment, though. pip install -r requirements.txt will install all the required packages listed in the file in one go.)

You can delete the whole /usr/local directory and the system won't be affected because /usr/local is the directory defined by the user solely. When you freshly install Ubuntu /usr/local is empty

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