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.
Related
I am trying to use Python3 with PyCharm.
So I installed Python(3.10.1) and PyCharm(2021.3.1)
But when I try to create a new project, PyCharm want me to choose 'base interpreter'
and I don't know which one to choose.
Here is the list of base interpreter:
I want to know what is difference between /usr/bin/python3 and Library/Frameworks/Python.framework/Versions/3.10/bin/python3
Usually the files under /usr/bin/ are symlinks to actual files, which means they are not real files but links to the actual file.
To see it for yourself go to the /usr/bin/ directory with a terminal and execute:
ls -a
I suppose macOS already ships with python and by installing Python 3.10 you added that last element to the list, which is also linked by /bin/usr/python3.
I'd go with /usr/bin/python3, more portable.
You can choose the one interpreter for the version of python you want. In your terminal, you can run python --version or python3 --version and it will display the version of python, Like this:
$ python --version
Python 2.7.16
$ python3 --version
Python 3.9.1
Performing which python, will give you the path to that python binary, like the following (although, this is where we are leaving "Python" and talking more about the OS and PATH.
$ which python3
/usr/local/bin/python3
For the other versions of Python you have installed, you can check their version by doing something like:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3 --version
You'll find that you may choose different versions of python for different projects. In the future, you may consider using venv to setup virtual python environments per project.
If you're just getting started with Python, I'd suggest sticking with the version you installed 3.10.1, and choosing that interpreter.
The base interpreter is the python.exe file that will run everything you do inside that project.
I would suggest you use Anaconda as a package manager and then create a virtual environment with the version of Python you need. Just follow these steps:
Getting started with Anaconda: https://docs.anaconda.com/anaconda/user-guide/tasks/pycharm/
Choosing an Anaconda virtual environment with Pycharm:
https://docs.anaconda.com/anaconda/user-guide/tasks/pycharm/
A package manager like Anaconda is very useful when working with Python.
Otherwise:
In your list you have many different versions of python. You should choose the base interpreter based on the python version you want to use.
Always select the latest version.
Since I use Python 3.9, it will be:
/usr/local/bin/python3.9
Based on your image, it would be:
/usr/local/bin/python3
The interpreter is what version your PyCharm project uses. It appears you have python2 and python3, so if you were writing in python2, you would use the python2 interpreter.
I would select the latest version, which in your case would be:
/usr/local/bin/python3
There is a default python version, namely python 2.6, on the GPU server with Linux OS. Now I want to install a new python version on the server from its source, namely python 2.7. I should not change the default python version since I am not the administrator and some reason. So what should I do?
You can install your new version of Python. It should be accessible with the python27 command (which may be a symbolic link).
Then you will just have to check that the python symbolic link still points to python26.
Doing this, python will keep on execute Python 2.6 while python27 will execute Python 2.7
You can use virtualenv, to execute your programm in an environment with python 2.7.
Install virtualenv and virtualenvwrapper (for comfotable use.)
mkvirtualenv -p <your-python-version> would then start a virtual environment where the desired python version is the default.
To build on Tryph's answer, you can install that new version to your home directory, then in a directory specified within your PATH (like in .bash_profile), you can point to that directory and within there create a sym-link that points to the new python.
For instance, if you have a bin folder in your home directory that is specified in the path
ln -s /bin/python ~/bin/python
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.
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 am trying to uninstall Python 3.2 and go back to 2.7, I do not have much experience with Python, and as I am learning now, it is becoming increasingly difficult to manage all of the work arounds needed to work with the newest version, and I require 2.7 for a college course.
I tried using the Windows 7 add/remove programs to uninstall Python 3.2 and 3.1, and then I downloaded and installed the 64 bit Python 2.7, but I was unable to open any of my already existing .py files with the executable in the Python27 folder.
When I click to open any .py file, I don't see any error messages but Windows asks what program to open the file with, and if I choose python.exe or pythonw.exe in C:\Python 27 I get a console Window appearing briefly and then closing. The "edit with IDLE" option in the context menu is gone, and if I try to edit with IdleX, I get another "what application to open with" window.
Instead of removing python 3.2, you can use both of python 2 and 3 in the same time. You just need to specify which version you want to use.
When in CMD, you can see the available versions installed on your windows. If it doesn't appear there, you need to install it.
py -0 # Prints out the versions you can use.
You can specify which python version you want to use.
For example in Windows 10, I use the code below for python 3.
py -3 fileName.py # runs using python 3
For python 2, you can use the code below after installing it.
py -2 fileName.py # Runs using python 2
Here is more information about installing more versions of Python in the same time.
How to install both Python 2.x and Python 3.x in Windows 7
Python 3.x is not backward compatible with Python 2.x, which was the purpose of the release. To clean up Python2.x without worrying about backward compatibility.
You can have as many python installations as you want on your computer, as they do not interact with each other. The python installations have it's own folder, with it's own idle, modules, launcher, ect.
You could install PyCharm or another Python IDE. It allows you to change which version of python you use to interpret your code. This will also identify syntax errors as you are writing and will notify you of them - in case you have code that works in 3.x but not 2.x.
Ecker00 is right, installing 2.7 in a separate directory gives you access to 2.7 while still having 3.x on your computer. You will have to re-install libraries with the Python27 folder's pip in order to use them in 2.7 though.
You can also change your python version using anaconda:
conda install python=3.5.0
or maybe
conda install python=2.7.8
or whatever you want.
Use the following steps to get it fixed.
//Check current Python pointer
ls -l python
//Check available Python versions
ls -l python*
//Unlink current python version
sudo unlink python
//Select required python version and lin to python command
sudo ln -s /usr/bin/python2.7 python
//Confirm change in pointer
ls -l python