Shell is selecting Python 2.7, but not 3.7 - python

I have Python 3.7 and I had a Python 2.7.
But when I am using Python Launcher it uses 2.7 not 3.7.
How to use Python 3.7? OS - Mac OS.

If you want python 3.7 as default when you run command python in terminal, you can add an alias in ~/.bashrc
alias python=python3.7
bashrc is a shell script that bash runs whenever it is started interactively. You can find bashrc in your home directory.

In the Python Launcher menu, go to Preferences. You should see a text box called "Interpreter", in there insert the path to your python 3.7, for example:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
You might also add that to your PATH environment variable and use python3 (and idle3) rather than plain python.

There are several ways you can obtain this behavior, my preferred one is symlinking the python3 executable to python
sudo unlink /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python
Another way would be using pyenv https://github.com/pyenv/pyenv
Be aware od the fact that if you do this and you have some scripts on your machine that use the shebang #! /bin/python and were written for py2, they may stop working

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

Simulate python3 shell commands behavior in windows powershell

I work on an api that run python3 scripts like python3 <scriptname.py>. This api initially run on a linux system. Unfortunately I make the local development of it on a windows. Is there a way to simulate this behaviour so that when I run in powershell python3 -V it give me the version of python.
I have allready python 3 installed on my computer. I tried to add the path to my python.exe to the system variables with for variable the string python3. But after registering and restarting powershell and type python3 -V I still get the error that «python3» is not recognized as a command applet name.
Does anyone have any ideas?
Once python is in your path, you can define a PowerShell alias for python3.
PS > New-Alias python3 python
PS > python3 -V
python 3.6.5
In order to have this alias as a permanent one, refer to this question: Permanent PowerShell Aliases.
I found tricky solution.
Once python is in your path, go to python installed path and copy python.exe and named duplicated one as python3.exe

How to tell the root process to use the anaconda python installation instead of /usr/bin/python?

I have a python script with the following shebang line on top:
#!/usr/bin/env python
I have Python3 installed on my Linux box and when I run this scripts as a regular users it correctly uses the Python3 that is located in my Anaconda installation:
/home/user/miniconda3/bin/python
However, when I tried to run this as root, it will use the old python in:
/usr/bin/python
How can I tell the root process to use the Anaconda python instead of the old default /usr/bin/python.
/usr/bin/env will read your current user's env variables. As your conda's python is in the PATH, so it will be used.
But root's env variables are different. The simplest solution would be change #!/usr/bin/env python to a specific python such as #!/home/user/miniconda3/bin/python. It will force to use this specific python version.

How to change python version in command prompt if I have 2 python version installed

I have installed Anaconda 2 & 3 in my system. Anaconda 2 contains python 2.7 & Anaconda 3 contains python 3.6.
I need to run my python code using command prompt & I need to use python 3.6
While I'm running python --version, I'm getting python 2.7.14. How do I change it to python 3.6?
As you can see, I have both Python2 and Python3 installed.
I hope you know that the path of the python executable has to be added to the PATH environment variable in Windows. As you can see, the path of Python2 is placed above the path of Python3 in my system.
How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM.
Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.
What seems like an obvious solution?
You might think, changing the name of python.exe to python3.exe for the Python3 executable will solve your problem. You are partially right, it will work. But you have to use python3 file.py or python3 --version, which I think, is understandable. But, pip will no longer work if you change the name of the original python executable. You will no longer be able to install external packages and modules.
How can this problem be solved?
You can create an alias for the Python3 executable called python3.bat.
.exe and .bat files can be called from the cmd directly without using their extension. We always write python filename.py instead of python.exe filename.py although both are correct. The same can be done with .bat files.
Go back to the first image and notice the python3.bat file below python.exe. That is my way of calling python3 without renaming my original python executable.
python3.bat
Create a new file using notepad or something and paste this %~dp0python %*
I don't fully understand how this works except that dp0 basically runs python from inside the same directory and %* passes all the arguments to the python executable. Place this file inside your Python3 installation directory and your problem will hopefully be solved.
python3 basically runs your python3.bat file, which in turn runs the python.exe from its folder and passes the arguments to it.
I hope this solves your problem.
You should have python3 also in your path. You could use python3 to run your script:
python3 <your_script>
But to answer your question, you could use alias and update it as python.
$ python --version
Python 2.7.6
$ alias python=python3
$ python --version
Python 3.6.4
If you want to run Python 3.6, then execute python3.6.
Otherwise, check with which python where the symbolic link to the actual python executable is. On my system, it's
/usr/bin/python
and when I ls -la /usr/bin/python, it gives
lrwxrwxrwx 42 user user 42 Aug 24 03:45 /usr/bin/python -> python2.7
If you are really sure that you want to do it, you can now just move the old symlink somewhere:
sudo mv /usr/bin/python /usr/bin/old_python
and create a new symlink:
sudo ln -s /usr/bin/python3.6 /usr/bin/python
Now you should get something like:
$ python --version
Python 3.6.5
I don't guarantee that it doesn't break everything in the world, especially if you have some distro that has a package manager that relies on a specific python version under python.
This seemed to work nicely on Linux Mint 18.
I made a small trick to solve this problem. For me
C:\Python27
and
C:\Python27\Scripts
were at the top of the PATH Environment variable list. I simply selected them and clicked on Moved Down to shift them to the end of the list.
Just make sure that your path of Python3 and its Script folder is above Python2 path.
And it solved the problem for me. Hope it would help you too.
Reason :
How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM. Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.
reason is taken from the accepted answer to this question.
In order to run your script with python 3 you can use
python3 <path to file>
P.S.: this should be a comment, but I do not have the required reputation. :)
If Anaconda 3's is the only python3 interpreter in the system you can run python3 instead of python in your command line and it should work.
You can alter PATH parameter inverting the positions of both interpreters.
I have two versions of the python which the first path is python 2(I am still working with python 2), but there are some scripts which should use python 3. This is what I have done:
I have create a bat file.
ex:
open Notepad++ and write :
#echo off
set PYTHONPATH=
"%~dpn0.py" %*
save it as 'yourscriptname.cmd'.
before my script(which should use python3) I just run this file in command line and then run my script. it is working.

Specify which version of Python runs in Automator?

In my terminal and in CodeRunner my Python is updated to 2.7.6 but when I ran a shell script in the OSX Automator I found that it is running 2.7.2
How can I update the Automator Python to 2.7.6 like the rest of my compilers ?
I couldn't specify explicitly which python for it to use.
So, I ran it in bash environment with following command:
$ your/python/path /path/to/your/python/script.py
And make sure first line of your python program contains the path to the python environment you wish to use.
Eg:
#! /usr/local/bin/python

Categories