Python3 install with nix mess PATH - python

I installed Python 3.6 on Debian 9 with
nix-env -i python3-3.6.2
but now my python interpreter has changed from the default Python 2.7 to this Python 3.6.2:
which python
/home/user/.nix-profile/bin/python
and this breaks a lot of stuff. How can I install Python 3.6 without replacing the standard Python interpreter?

That's right, python3 on Nixpkgs provides python executable. The root cause of this is that user installed programs hide system-wide programs (see your PATH envvar).
You should install python2 too, and solve the priority override for python executable.
But you may be interested in nix-shell -p python3 instead, which creates temporal environment with python3 and leaves python 2 elsewhere.

Related

Installed Python 3.8 on Ubuntu, but it's not the default

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

Installed Python 3.7 and tried changing that to the default however, changing $PATH and running python --version still shows 2.7

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

What file does the python symlink point to

I bought my mac about a year ago and somehow changed my python symlink so that when I run python some_file.py, python 3.4 is used to run the file instead of python 2.7. I now need to change it back, but I can't figure out what I did to change it in the first place! When I run:
import os
os.path.realpath("/usr/local/bin/python")
in the python terminal, the output is:
'/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
Does this not mean that my python symlink is pointing to my python 2.7 version, and not my 3.4 version? If not, how do I find out which file is run when I use the python symlink?
You probably installed that specific Python version using the official Python installer for OS X; see the Using Python on a Macintosh documentation. The installer creates the /usr/local/bin symlink for you.
If you also, at some point, had 3.4 installed then that installation is still there too. Check for a /usr/local/bin/python3 command; it'll link to the existing Python 3 binary. Use that instead to run Python 3 code.
If you do have a /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 command, you could re-create the /usr/local/bin/python symlink to point there instead, but I'd personally only use the python3 name for Python 3 scripts.
Last, you could also have used the homebrew tool to install Python; it can manage symlinks for you. However, homebrew installs Python binaries into the /usr/local/Cellar tree structure instead.

Is it safe to set python bin in $PATH to another python version?

I have installed Anaconda3 just now, and I noticed that now, when I run python command from terminal, Python 3.5.1 |Anaconda 4.0.0 (64-bit)| is starting. Anaconda installer had added path to anaconda dir in $PATH and there is symlink from python to python3.5
My question is: will programs, that depends from python command and expects python2, work correctly, or I should remove symlink python from anaconda dir?
My question is: will programs, that depends from python command and
expects python2, work correctly?
Those programs should use full path of the python binary. Something like /usr/bin/python, and so $PATH is irrelevant. As long as you don't change /usr/bin/python, nothing will break.
If you remove the stuff that Anaconda has added, it's likely that Anaconda will not work properly.
That depends on your OS. Debian and Ubuntu both have ongoing projects to move the "default" version from 2 to 3 (also here). But it's not recommended to point /usr/bin/python to python3 if Python 2 is installed (see PEP 394).
If you want 'python' to be pointing to your 3.x install, you could use an alias (see here). This way you can use python in your session and at least don't change it on the whole system.

Switch between python 2.7 and python 3.5 on Mac OS X

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.)

Categories