I have downloaded Python 3.8 on my Ubuntu 16.04 and if i write python3.8 it shows it is present, but when I write python --version I am getting my old Python version which was 3.5
Set an alias:
alias python=python3.8
Then running python in Bash will run python3.8.
To make the change permanent, put the alias line in ~/.bashrc. It will take effect when you open Bash.
Symlinks
Why not symlink /usr/bin/python to python3.8?
Ubuntu follows PEP 394 which says the python command should point to Python 2. However an alias works since it only affects your shell. It doesn't even affect scripts you write, meaning if you want a script to run in Python 3.8, you'll have to write it explicitly in the shebang, e.g. #!/usr/bin/env python3.8 instead of #!/usr/bin/env python.
Why not at least symlink /usr/bin/python3 to python3.8?
Some things will break since there are version-specific libraries. For example _gi is not available for Python 3.8 on Ubuntu 16.04, so Gnome Terminal will not open. See Gnome terminal will not start on Ask Ubuntu for an example.
More details
How to make 'python' program command execute Python 3? on Ask Ubuntu
Everything depends on how you installed the python3.8.
Many methods wouldn't update default symlinks for you.
If you do something like:
sudo ln -s /usr/bin/python3.8 /usr/local/bin/python and run python --version afterward it should solve your issue.
If you python3.8 binary is not in /usr/bin/python3.8 update your symlink path accordingly.
Keep in mind that some apps dependent on specific features of the lower python version might not work correctly. With Python3 the probability is low though.
The correct way is sudo apt install python-is-python3 - it effectively does a symlink, but it also keeps pace with future updates; so if your ubuntu distribution moves to say Python 3.9, the manual symlink will no longer work, but the package makes sure it is still valid.
credit to https://askubuntu.com/a/1272899/1055134
Related
Sorry if the question is simple or I'm missing something obvious but I'm fairly new with console commands and Python.
As the title suggests, I installed Homebrew and subsequently the latest version of Python. I want to make this the default when running python --version in bash.
I tried changing the path in my bash_profile with the following line at the bottom:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
It seems that when I install new modules however, that they are for 2.7 and not 3. Checking pip --version going straight to the default:
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
I also tried running the following commands:
echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
this does edit my bash_profile again, but the line I wanted (and added manually) isn't the one that appears; instead the following is inserted (I have no idea why Applications/VMware is inserted at the end, it has nothing to do with Python or what I'm trying to do):
export PATH=/usr/local/bin:/usr/local/sbin:$PATHexport Path=/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware
Can anyone explain to me how I can make python3 the default. I don't want to get rid of 2.7 obviously because a lot of scripts for mac still depend on it. Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
You really don't want to change this.
First:
Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
Yes! Install pipenv, and set up an environment where 3.7 is the default, but leave the system default at 2.7.
More generally:
According to PEP 394, which isn't due to be reevaluated until 2020, if you don't have a virtual environment activated:
python3 runs Python 3.x
python2 runs Python 2.x
python usually shouldn't be used—but if you do use it, it should run Python 2.x.
Usually, you'll also have, e.g., python3.7 and python2.7 to run specific 3.x and 2.x versions, although this isn't covered by any standard.
Scripts like pip, and those installed by pip, should be installed with similar suffixes—but ideally, if you have multiple versions, you shouldn't run those scripts. Most of them are designed to be run as modules, so you can run python3 -m pip to run the pip for your 3.x.
For a few things, it may be handy to set up shortcuts. (For example, instead of running python3 -m ipython from the command line, I usually use an iTerm profile that uses that instead of bash as my start command. If you don't use either IPython or iTerm, you can make a Terminal.app profile that runs python3.)
Scripts installed as part of your platform's package manager are a whole more complicated story, but macOS doesn't have an official package manager, and Homebrew generally expects you to install Python libraries with pip, not brew, so you don't have to worry about that.
Programs should have an installer—whether they install via pip or something like py2app—that creates a proper shbang line, so they run with whichever Python version they were installed with, so you don't have to worry about them. But programs that come with your OS may use something like /usr/bin/env python, and they will expect that to run the version of Python that came with your OS.
For development purposes, you usually want to use virtualenv or pipenv or conda so that python runs the Python interpreter for whichever environment is currently activated.
So, don't try to change the fact that python runs Apple's Python 2.7. That's what it's supposed to do, and if you make it do otherwise, you could break things. Use python3, python3 -m pip, etc.; use #!/usr/bin/env python3 on your scripts (or, better, use setuptools to create entry-point scripts automatically); use virtual environments; just never run python, and everything will work properly.
You just need to run command
$which python
And then goto the path rename python to python2
Then run command
$which python3
To get the path of python3
Just link with command
sudo ln -s $python3path /usr/bin
/python
You need to instead $python3path with real path
Then you can run python3 with python command
I generally use Python 2.7 but recently installed Python 3.5 using Miniconda on Mac OS X. Different libraries have been installed for these two versions of python. Now, the entering either of the keywords 'python' or 'python3' in terminal invokes python 3.5, and 'python2' returns '-bash: python2: command not found'. How can I now invoke them specifically using aliases 'python2' and 'python3' respectively?
I am currently using OS X El Capitan.
IMHO, the best way to use two different Python versions on macOS is via homebrew. After installing homebrew on macOS, run the commands below on your terminal.
brew install python#2
brew install python
Now you can run Python 2.7 by invoking python2 or Python 3 by invoking python3. In addition to this, you can use virtualenv or pyenv to manage different versions of python environments.
I have never personally used miniconda but from the documentation, it looks like it is similar to using pip and virtualenv in combination.
OSX's Python binary (version 2) is located at /usr/bin/python
if you use which python it will tell you where the python command is being resolved to. Typically, what happens is third parties redefine things in /usr/local/bin (which takes precedence, by default over /usr/bin). To fix, you can either run /usr/bin/python directly to use 2.x or find the errant redefinition (probably in /usr/local/bin or somewhere else in your PATH)
I already had python3 installed(via miniconda3) and needed to install python2 alongside in that case brew install python won't install python2, so you would need
brew install python#2 .
Now alias python2 refers to python2.x from /usr/bin/python
and alias python3 refers to python3.x from /Users/ishandutta2007/miniconda3/bin/python
and alias python refers to python3 by default.
Now to use python as alias for python2, I added the following to .bashrc file
alias python='/usr/bin/python'.
To go back to python3 as default just remove this line when required.
Similar to John Wilkey's answer I would run python2 by finding which python, something like using /usr/bin/python and then creating an alias in .bash_profile:
alias python2="/usr/bin/python"
I can now run python3 by calling python and python2 by calling python2.
Here is how to set the Python version back to 2.7 if you have installed Anaconda3 (Python 3.6) on MacOS High Sierra 10.13.5.
Edit the .bash_profile file in your home directory.
vi $HOME/.bash_profile
Comment out the line with anaconda3 addition to your PATH like this:
# export PATH="/Users/YOURUSERNAME/anaconda3/bin:$PATH"
Close the shell, then open it again
You should now see 2.7 when you run python
Then, if you want 3.6 you can simply uncomment out the anaconda3 line in your .bash_profile.
Trying to unlink python will end in tears in Mac OS X.
You will get something like this
unlink: /usr/bin/python: Operation not permitted
I just follow up the answer from #John Wilkey.
My alias python used to represent python2.7 (located in /usr/bin).
However the default python_path is now preceded by /usr/local/bin for python3; hence when typing python, I didn't get either the python version.
I tried make a link in /usr/local/bin for python2:
ln -s /usr/bin/python /usr/local/bin/
It works when calling python for python2.
If you want to use Apple’s system install of Python 2.7, be aware that it doesn’t quite follow the naming standards laid out in PEP 394.
In particular, it includes the optional symlinks with suffix 2.7 that you’re told not to rely on, and does not include the recommended symlinks with suffix 2 that you’re told you should rely on.
If you want to fix this, while sticking with Apple’s Python, you can create your own symlinks:
$ cd <somewhere writable and in your PATH>
$ ln -s /usr/bin/python python2
Or aliases in your bash config:
alias python2 python2.7
And you can do likewise for Apple’s 2to3, easy_install, etc. if you need them.
You shouldn’t try to put these symlinks into /usr/bin, and definitely don’t try to rename what’s already there, or to change the distutils setup to something more PEP-compliant. Those files are all part of the OS, and can be used by other parts of the OS, and your changes can be overwritten on even a minor update from 10.13.5 to 10.13.6 or something, so leave them alone and work around them as described above.
Alternatively, you could:
Just use python2.7 instead of python2 on the command line and in your shbangs and so on.
Use virtual environments or conda environments. The global python, python3, python2, etc. don’t matter when you’re always using the activated environment’s local python.
Stop using Apple’s 2.7 and instead install a whole other 2.7 alongside it, as most of the other answers suggest. (I don’t know why so many of them are also suggesting that you install a second 3.6. That’s just going to add even more confusion, for no benefit.)
I would like to update Python 2.7 to Python 3.4. Currently, I am using Mac OS X 10.9.5.
I have used Homebrew to install Python 3.4. However, when I used python -V to check the version of my Python in the terminal, it still appears to be on the 2.7 version.
Anybody have any idea as for why this is?
Also, I have tried to follow the directions of this article to update to Python 3 (see the second half of the article). Yet my system cannot find this file: /System/Library/Frameworks/Python.framework/Versions/3.4
First install python3 using homebrew.
brew install python3
Second you may want to use python3 as default change the ~/.bash_aliases to point to python3. This can be done by adding the following command in the ~/.bash_aliases.
alias python='python3'
now all the python command gets referred to python3 by default.
EDIT 1:
Try running the following command.
/usr/bin/env python3
This will make python3 as default environment for python executables.
EDIT 2:
[Like suggested by robert]
try making a symlink from python to python3. this makes python3 as default environment in all shells.
I used the installer from http://www.python.org/download. The install appeared successful, and it dropped the Python 3.3 folder in my Applications directory. I ran the "Update Shell Profile.command" script it contained, and it prepended /Library/Frameworks/Python.framework/Versions/3.3/ to my path. Yet the Python version in that directory appears to be 2.7.5.
/Library/Frameworks/Python.framework/Versions/3.3 ls
Headers Python Resources bin include lib share
/Library/Frameworks/Python.framework/Versions/3.3 Python --version
Python 2.7.5
Also, ls /usr/bin | grep python shows:
python
python-config
python2.5
python2.5-config
python2.6
python2.6-config
python2.7
python2.7-config
pythonw
pythonw2.5
pythonw2.6
pythonw2.7
What have I missed?
There are multiple problems here.
First, you should not be running Python, the framework's executable. Framework executables aren't meant to be run, and it's really only a coincidence that this one happens to work.
Frameworks with programs meant to be run put them in a bin directory somewhere—either outside the framework (like /usr/local/bin) or inside it (like /Library/Frameworks/Foo.framework/Versions/X.Y/bin). By default, Python 3.3 does the latter.
And the programs inside the bin directory are all lowercased, not capitalized, so there is no Python to run.
Next, on Mac, and on almost every other platform in the world besides Windows, the current working directory is not part of your PATH. So, when you type Python --version, that finds Python somewhere on the PATH. The fact that you happened to have an executable of that name in the current directory doesn't mean anything (except that it's confusing to you). If you really want to run that file (but again, you really don't), you have to write ./Python instead.
Also, there is really no good reason to cd into the framework directory in the first place. Sure, you could run the file you want, from there, with the appropriate relative pathname: bin/python3, for example, but you don't want to.
Next, likely you're using a shell you already had running before installing Python 3.3. The Update Shell Profile.command script can be used to add Python 3.3 to the PATH for all future shells, or to spawn a new shell with that PATH, but either way it will not affect any existing shells. To solve that, you just have to start a new shell.
Next:
ls /usr/bin | grep python shows:
The /usr/bin directory is only for programs that are part of the OS. Programs you install yourself go in /usr/local/bin, or somewhere else on your PATH, instead. The Python installer has an option (although it may be hidden, I can't remember…) to put launchers in /usr/local/bin. And it also has an option—which you selected—to put its framework bin directory onto your PATH. But either way, it's never going to put anything in /usr/bin.
And finally, even after installing Python 3.3, the default python will still be 2.7. python3 and python3.3 will be 3.3, but python and python2.7 will be 2.7. See PEP 394 — The "python" Command on Unix-Like Systems for the rationale. But the short version is, there's all kinds of code that depends on Python 2.7 and isn't compatible with 3.3 that may be installed on your system, and you don't want it all to stop working.
So, putting it all together:
Create a new tab or window in Terminal.app.
Type python3 --version.
You may want to consider using a virtualenv:
$ /Library/Frameworks/Python.framework/Versions/3.3/bin/python3 -m venv ~/myvenv
$ source ~/myvenv/bin/activate
(myvenv) $ curl https://bootstrap.pypa.io/get-pip.py | python
(myvenv) $ deactivate
$ source ~/myvenv/bin/activate
http://docs.python-guide.org/en/latest/dev/virtualenvs/
I came from a Windows background whern it comes to development environments. I'm used to run .exe's from everything I need to run and just forget.
I usually code in php, javascript, css, html and python.
Now, I have to use Linux at my work, in a non changeable Ubuntu 8.04, with permissions to upgrade my system using company's repositories only.
I need to install Python 2.4.3 to start coding in an old legacy system. I had Python 2.5. I downloaded Python 2.4.3 tarballs, ran ./configure make and such. Everything worked out, but now the "default" installation is my system is Python2.4 instead of of Python2.5.
I want help from you to change it back, and if possible, some material to read about symlinks, multiple Python installations, virtualenvs and such: everything I need to know before installing/upgrading Python modules. I installed for example the ElementTree package and don't even know in which Python installation it was installed.
Thanks in advance!
You may have installed Python 2.4 in /usr/local/bin, which, in turn, may come in your $PATH before /usr/bin where 2.5 lives. There are various possible remediations, if that is the case: simplest is probably to rm the link named /usr/local/bin/python (leaving only the "system" one named /usr/bin/python). You will then have to use explicitly python2.4 to invoke the 2.4 installation, while just python will go to the system-installed Python 2.5 installation.
If you have root access you could just create a new simlink.
sudo mv /usr/bin/python /usr/bin/python2.4
sudo ln -s /usr/bin/python25 /usr/bin/python
I don't have too much experience with ubuntu, but i guess it shouldn't brake anything.
To learn more about ln read man ln.
For which version of Python will run when you invoke the python command you will have to manually change the symlink that /usr/bin/python points to, but that won't change what the packaging system considers the "default version of Python" and means you will still have to install version-specific libraries if they are different for a specific version. Luckily, those packages have an easy naming convention, instead of just python-<foo> they are python2.4-<foo> and installing those will put them in the right path (specifically the right site-packages directory).
EDIT: apparently python isn't managed by the alternatives system, silly Debian/Ubuntu
Running
sudo apt-get install --reinstall python-minimal python python2.5
should restore the default Python installation.
Unlike Windows Ubuntu comes with quite a lot of software packaged by the distributor, and it is a good idea to stay with this packages if possible instead of downloading software from the net. Ubuntu 8.04 has Python 2.4.5 (package python2.4), maybe that works for you.
If you need to install Python from source use
./configure --prefix=/usr/local/
instead of a plain ./configure. This makes python to be install at /usr/local/ so it doesn't overwrite the distribution's files
Piggybacking off of #rebus:
sudo ln -s /usr/bin/python2.5 /usr/bin/python
Seems to have worked.