Start the correct python program via symbolic link - python

I recently installed ActivePython 2.7. So I've got a symbolic link in /usr/local/bin/python which points to the 2.7 directory. Trouble is, when I type "python" at the command line, it starts up MacPython 2.6 in /usr/bin/python. I don't understand why the symbolic link is not taking precedence. /usr/local/bin is on my PATH. Any help would be appreciated.
~ $ which python
/usr/bin/python
~ $ find /usr -iname python
/usr/bin/python
/usr/local/bin/python
Path looks like this:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/opt/PalmPDK/bin:/opt/PalmSDK/Current/bin:/usr/X11/bin

When you type “python” on the command line, the shell checks the values in your $PATH from left to right, and invokes the program it finds first. Check your current $PATH setting like this:
$ echo $PATH
If it contains both /usr/bin and /usr/local/bin then whichever comes first will take precedence.

in your PATH variable, /usr/bin probabbly is before the /usr/local/bin/ directory. you should use the full path in order to work in this case.

Related

Make python3 as my default python on Mac

What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.
The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
And then I did brew install python, and was hoping to see python3 when I do python --version.
But it just shows me python 2.7.10. I want my default python to be python3 not 2.7
And I found a little clue from the website.
Do I have a Python 3 installed?
$ python --version
Python 3.6.4
If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/
Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --version
Python 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
Open Terminal
cd
nano ~/.bashrc
alias python=python3 (Add this line on top of .bashrc file)
Press ctr+o (To save the file)
Press Enter
Press ctr+x (To exit the file)
source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)
python --version
Python 3.7.5
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.
If you are using zsh (the default on Mac OS) run the following from terminal:
echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc
According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.
The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.
Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".

brew-installed Python not overriding system python

I just used brew to install Python 3 on OS X. The python3 command now starts the interpreter using brew Python 3.6, but python still opens the interpreter with the default system Python 2.7.
My understanding was that, by default, brew Python should now override system Python. (I.e., see Order of /usr/bin and /usr/local/bin and more in $PATH). In my PATH, /usr/local/bin comes before /usr/bin, so it shouldn't be a PATH issue. I have tried restarting Terminal, with no effect.
Here is my full PATH in case that is relevant.
/Users/**/.rvm/gems/ruby-1.9.3-p362/bin:/Users/**/.rvm/gems/ruby-1.9.3-p362#global/bin:/Users/**/.rvm/rubies/ruby-1.9.3-p362/bin:/Users/**/.rvm/bin:/Users/**/.rvm/bin:/Users/**/Python/PmagPy/programs/conversion_scripts2/:/Users/**/Python/PmagPy/programs/conversion_scripts/:/Users/**/Python/PmagPy/programs:/usr/local/heroku/bin:./bin:/usr/local/sbin:/usr/local/bin:/usr/local/share/npm/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
Why isn't brew Python taking precedence? And how can I fix (or troubleshoot) this? If I can't find another option, I can create an alias, but I prefer to understand what's happening and get to the root of the problem.
Update:
I checked out the "possible duplicate" question, but my issue doesn't appear to be a linking problem:
~ brew link --overwrite --dry-run python
Warning: Already linked: /usr/local/Cellar/python/3.6.4_4
To relink: brew unlink python && brew link python
~
TL;DR Add the following to your .bash_profile (or equivalent):
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
Explanation
It seems python via homebrew is now handled differently (see https://docs.brew.sh/Homebrew-and-Python).
python3 points to Homebrew’s Python 3.x (if installed)
python2 points to Homebrew’s Python 2.7.x (if installed)
python points to Homebrew’s Python 2.7.x (if installed) otherwise the macOS system Python. Check out brew info python if you wish to add
Homebrew’s 3.x python to your PATH.
Checking out brew info python hints at what you need to do:
Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been
installed into /usr/local/opt/python/libexec/bin
The hint being that you therefore have to add /usr/local/opt/python/libexec/bin before /usr/bin in your path (not /usr/local/bin as stated in some sources e.g. https://docs.python-guide.org/starting/install3/osx/)
See also https://github.com/Homebrew/homebrew-core/issues/15746
One-liner to get homebrew python working:
zsh
echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc
bash
echo -n 'export PATH="/usr/local/opt/python/libexec/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc
Explanation:
>> filename appends at the end of the file
source filename reloads the file
I tried a few of the proposed solutions in How to link home brew python version and set it as default, but none of them worked. Ultimately I solved this by symlinking python3 --> python:
ln -s /usr/local/bin/python3 /usr/local/bin/python

bad interpreter no such file or directory /usr/bin/python

I created script python and I moved it to /usr/bin and I named the script by sdfgdgh without .py and I write in script this code
#! /usr/bin/python
print("worked")
and I was given the script chmod +x
but when I type in terminal sdfgdgh give me the error :
bad interpreter no such file or directory /usr/bin/python
why and what is the solution ?
The problem is with your python installation. Probably your /usr/bin/python either does not exist at all or it is a dead symbolic link pointing to non-existing python.
So first solution is to check if /usr/bin/python exists. If so check if it's not dead link and if it is, fix the link to point to existing python intepretter:
cd /usr/bin
sudo ln -fs <full_path_to_existing_python_binary> python
If you can't or don't want to change /usr/bin/python but you have python installed and its location is recognized by the system (i.e. calling python from shell works) you can try changing your script as a workaround:
#! /usr/bin/env python
print("worked")
This way your script will use python as an interpreter regardless of the real python location as long as it is in your PATH.
I had similar problems, I had installed a package in my Ubuntu 20.04 which depended on Python 2. This messed up the meaning of python. I had to uninstall that package: qjoypad, xboxdrv and one other; uninstall python 2 with sudo apt remove python.
Then to confirm, I used which python which gave a blank output. The next step was to cd /usr/bin and then create a symlink with sudo ln -fs python3 python.
First check which python you've installed with
$ which python
/usr/bin/python
Then check if it's executable
python -V
Python 2.7.5
If you run a py file with dos format on linux you also will get this issue.
Method a:
check dos fileformat with cat -v filepath to see if the line end with ^M.
Method b:
vim filepath -> :set ff to check it, for example "eni.py" [dos] 64L, 2151C
Method c:
file filepath to check if it has CRLF
file_path: Python script, UTF-8 Unicode text executable, with CRLF line terminators
Solution:
you can set the filefort to unix with vim filepath then :set ff unix
You can install python with:
apt install python
After that, the python command will work.
A flexible solution would be to point at this address using the address of an arbitrary python version.
Assuming you are using Ubuntu, you can find the installed Python versions with
ls /usr/bin | grep python
For me this printed:
dh_python2
python
python2
python2.7
python3
python3.8
python3.8-config
python3-config
python3-futurize
python3-pasteurize
x86_64-linux-gnu-python3.8-config
x86_64-linux-gnu-python3-config#
Now let's say you want to point to python 3.8. The following line of code presents python3.8 as the 1st alternative(Hence the 1 at the end).
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
Now whenever the python is referred, instead of the /usr/bin/python folder, /usr/bin/python3.8 will be accessed.
You can enlist other alternatives too. To see which alternatives you have, use
update-alternatives --list python
Finally to switch between those alternatives, use
sudo update-alternatives --config python
I was encountering the same problem. Maybe you are editing the script on windows as a file and executing it on Linux. below steps resolved the problem for me:
edit on terminal-> sudo vi myscript.py
add python location-> #!/location/anaconda/bin/python
From the ^M you can see that the file myscript.py is using windows/dos-style line breaks not Linux style
I know my answer is late but, I believe it can be of help to someone.
First: Check which python version you are using. which python3
Second: This will print the exact path for you. /usr/bin/python3
Third: Copy the path and use in your script. #! /usr/bin/python3
I got this error when I ran virtualenv ven3 twice by mistake, as it corrupted the ven3 directory.
Simply run this :-
sudo bash -c "test -e /usr/bin/python || (apt -qqy update && apt install -qy python-minimal)

change python version in terminal and intelliJ

short Q,
In a mac OS sierra terminal,
If I do:
whereis python
/usr/bin/python
Then if I do:
/usr/bin/python it opens python 2.10
but if I execute
python it opens python 2.7.8.
and this one comes from
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
How do I change the default python to point to python 2.7.8? In the terminal and in intelliJ?
Check the PATH environment variable with
echo $PATH
The python version you get when typing bare 'python' will be the first one found in that list of directories.
It is possible to control which python version is launched by, for example, rearranging the entries in PATH or by adding a symbolic link to the desired version in a position before the current version.
However, a more popular way to manage multiple python versions on the same machine is to use virtualenv. This will give you much less headaches when using pip to install/uninstall packages for particular python versions.
As wim mentioned you will get the first python that is found in $PATH.
A nice way may be to ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python. This will create a symbolic link in /usr/local/bin
That way you don't change the order in your $PATH variable. Also note that if you echo $PATH, /usr/local/bin should be before /usr/bin - in case you have other versions in /usr/bin (which you do given your example)
I would strongly recommend you do what wim mentioned and use virtualenv to manage you packages.

How do I change where Bash looks for Python in Linux?

I just updated my ReadyNas from python 2.3.5 to python 2.6.6. The upgrade placed the new version in the /usr/local/bin directory. So
/usr/local/bin/python is Python 2.6.6
/usr/bin/python is Python 2.3.5
When I type python at a bash prompt tries to run /usr/bin/python or my old version. I relocated my old version, and now I get:
bash: /usr/bin/python: No such file or directory
How can I change where bash looks for python? How is bash currently deciding that when I type python that it only looks in /usr/bin for python?
Your PATH environment variable. It has a list of directories which bash searches (in the same order) when it's looking for an program to execute. Basically you want to put /usr/local/bin at the start of your PATH environment variable. Add the following to your ~/.bashrc file:
export PATH=/usr/local/bin:$PATH
You can have a look at the current setting by running the set command in bash.
Alternatively, you can simply rename /usr/bin/python to /usr/bin/python2.3 and create a symlink pointing to the new version, e.g.
ln -s /usr/local/bin/python /usr/bin/python
I don't think it's BASH responsibility to choose the default version for the Python interpreter.
If you're the administrator, the cleanest way to do this is to use a symbolic link in /usr/bin/python pointing to the appropiate version. Avoid replacing the actual binaries if possible.
If you're not, then add a bin folder somewhere you have access to, and prepend it to the $PATH environment variable. Then create a symlink to the desired version of the python interpreter.
Cheers!

Categories

Resources