bad interpreter no such file or directory /usr/bin/python - 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)

Related

exec: "python": executable file not found in $PATH

since the last update to Mac OS Monterey 12.3 I get the following error message when compiling my Arduino sketch:
exec: "python": executable file not found in $PATH
Unfortunately, I have not yet been able to find out how to solve this problem.
I would be very grateful for ideas and suggestions.
Four steps are needed:
Install python3 using i.e. Brew:
brew install python
python3 is in:
/opt/homebrew/bin/python3
Link Python to python3:
sudo ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python
Check if you can execute it from the terminal; i.e.,
python --version
Irrespective of whether you use python --version or python3 --version, it should show python 3 now.
Open terminal and execute:
open /Applications/Arduino.app
It works in my case. It looks like when Arduino is executed from the GUI, it does not read the $PATH properly, so although python is linked to python3, it does not find it.
Problem
In MacOS 12.3 Apple removed python2.7 (python) from MacOS.
Solution
What I did to solve this is link python3 to python, I wouldn't recommend it because it's sus, I would recommend you wait until Arduino IDE fixes this issue in a later build. For the time being, you could try their Web IDE: Arduino Editor
However, here are the instructions to link python3 to python:
If you don't have python3 installed, install it here in the link below:
Python Install Page
Find your path for the current version of python3 you're using
which python3
it'll show up with something like this:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3
Copy that and use it to run this command that links python 3 to python. Replace the first file path with where your python3 is.
ln -s -f INSERT_PATH_OF_PYTHON3 /usr/local/bin/python
for example:
ln -s -f /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 /usr/local/bin/python

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

brew install python installs python2

I have 3 versions of python installed on my Mac. 2 of them are through brew i.e. python2 and python3 while the native version is python. the problem is when I put brew install python it installs python2 and not python. (By saying python means the version which runs on putting that command in terminal). What should I do so that if I type python my brew installed python launches.I have my path variables set correctly and the brew installation path is ahead than that of the usr/bin The problem I am encountering is that I have nltk installed through pip, pip2 and pip3 and when I import nltk in python2 and python3 there is no problem but when I do that in python it show no module found.
Try which python in a terminal to see which python will run. Then you know and can act accordingly to fix it.
ls -lsa $(which python) will let you see if it is a symlink to another location or a real executable. if a Symlink you can see where it points to and so you can follow the breadcrumbs to the final binary used.
if you run python from a terminal and in the python REPL do the following
import sys
print sys.path
you can see to which site-packages locations are pointed.
Other than that you of course have the option to use virtual environments to set up your version of python
if you really want python2 to be the default python command you can add a symlink to your ~/bin folder (create it if it does not exist)
mkdir ~/bin
cd ~/bin
ln -s $(which python2) python
chmod +x python
and make sure that export PATH=~/bin:$PATH is added at the back of your .bashrc or .profile or .zshrc file
Now start a new terminal session and try out python again it should point to brews version
Hope that helps

How to set Python's default version to 3.x on OS X? [duplicate]

This question already has answers here:
How to change default Python version?
(19 answers)
Closed 1 year ago.
I'm running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default.
Currently:
$ python
version 2.7.5
$ python3.3
version 3.3
How do I set it so that every time I run $ python it opens 3.3?
Changing the default python executable's version system-wide could break some applications that depend on python2.
However, you can alias the commands in most shells, Since the default shells in macOS (bash in 10.14 and below; zsh in 10.15) share a similar syntax. You could put
alias python='python3'
in your ~/.profile, and then source ~/.profile in your ~/.bash_profile and/or your~/.zsh_profile with a line like:
[ -e ~/.profile ] && . ~/.profile
This way, your alias will work across shells.
With this, python command now invokes python3. If you want to invoke the "original" python (that refers to python2) on occasion, you can use command python, which will leaving the alias untouched, and works in all shells.
If you launch interpreters more often (I do), you can always create more aliases to add as well, i.e.:
alias 2='python2'
alias 3='python3'
Tip: For scripts, instead of using a shebang like:
#!/usr/bin/env python
use:
#!/usr/bin/env python3
This way, the system will use python3 for running python executables.
You can solve it by symbolic link.
unlink /usr/local/bin/python
ln -s /usr/local/bin/python3.3 /usr/local/bin/python
Open ~/.bash_profile file.
vi ~/.bash_profile
Then put the alias as follows:
alias python='python3'
Now save the file and then run the ~/.bash_profile file.
source ~/.bash_profile
Congratulation !!! Now, you can use python3 by typing python.
python --version
Python 3.7.3
I encountered this issue as well, so I thought I should post an updated answer. Please note that this will only apply to a Mac-based setup (I haven't tried it with Windows or any flavor of Linux). The simplest way to get this working is to install Python via Brew. If you don't have brew installed, you will need to do that first. Once installed, do the following in at the terminal:
brew install python
This will install Python 3. After it's installed, run this:
ls -l /usr/local/bin/python*
You will see all of the links created by brew to its Python install. It will look something like this:
lrwxr-xr-x 1 username admin 36 Oct 1 13:35 /usr/local/bin/python3# -> ../Cellar/python/3.7.4_1/bin/python3
lrwxr-xr-x 1 username admin 43 Oct 1 13:35 /usr/local/bin/python3-config# -> ../Cellar/python/3.7.4_1/bin/python3-config
lrwxr-xr-x 1 username admin 38 Oct 1 13:35 /usr/local/bin/python3.7# -> ../Cellar/python/3.7.4_1/bin/python3.7
lrwxr-xr-x 1 username admin 45 Oct 1 13:35 /usr/local/bin/python3.7-config# -> ../Cellar/python/3.7.4_1/bin/python3.7-config
lrwxr-xr-x 1 username admin 39 Oct 1 13:35 /usr/local/bin/python3.7m# -> ../Cellar/python/3.7.4_1/bin/python3.7m
lrwxr-xr-x 1 username admin 46 Oct 1 13:35 /usr/local/bin/python3.7m-config# -> ../Cellar/python/3.7.4_1/bin/python3.7m-config
The first row in this example shows the python3 symlink. To set it as the default python symlink run the following:
ln -s -f /usr/local/bin/python3 /usr/local/bin/python
You will have to reload your current terminal shell to use the new symlink in that shell. Run this command to reload your shell:
exec $SHELL -l
You're all set now. Now, you can do:
which python
and it should show:
/usr/local/bin/python
All newly opened shell sessions will (should) automatically use the new symlink. To test this, open a new terminal shell and run the following:
python --version
Go to terminal type:
alias python=python3.x
This will setup default python as python3.x
This worked for me. I added alias and restarted my terminal:
alias python=/usr/local/bin/python3
The following worked for me
cd /usr/local/bin
mv python python.old
ln -s python3 python
Go to 'Applications', enter 'Python' folder, there should be a bash script called 'Update Shell Profile.command' or similar. Run that script and it should do it.
Update: It looks like you should not update it: how to change default python version?
I believe most of people landed here are using ZSH thorugh iterm or whatever, and that brings you to this answer.
You have to add/modify your commands in ~/.zshrc instead.
$ sudo ln -s -f $(which python3) $(which python)
done.
Mac users just need to run the following code on terminal
brew switch python 3.X.X
3.x.x should be the new python version.
This will update all the system links.
UPDATE
For Newer version of MAC use
brew link python 3.X.X
Suggestions to alias python to python3 will cause problems with virtual environments that set the version of python (eg: pyenv). With pyenv, you can set the version globally like so:
pyenv global 3.8.2
and then in any specific project, you can create a .python-version file which has the python version inside of it:
pyenv local 2.7.1
This is the best way to manage multiple versions of python on a system in my opinion.
I think when you install python it puts export path statements into your ~/.bash_profile file. So if you do not intend to use Python 2 anymore you can just remove that statement from there. Alias as stated above is also a great way to do it.
Here is how to remove the reference from ~/.bash_profile
- vim ./.bash_profile
- remove the reference (AKA something like: export PATH="/Users/bla/anaconda:$PATH")
- save and exit
- source ./.bash_profile to save the changes
On MacOS
Step-1: Upgrade python to latest version by:
$ brew upgrade python
Step-2: Go to home:
$ cd
Step-3: open .bash_profile
$ vi .bash_profile
Setting PATH for Python 3.8
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
Step-4: Save the file. And compile it by:
$ . .bash_profile
Step-5: Check the python version:
$ python -V
Step-6: Thats all.
This is the simplest way from my exp. (if you have brew installed on your mac).
Try this from your terminal:
brew install python3
and then run the below on your terminal :
ls -l /usr/local/bin/python*
Tip:
** (note down the python version 3.8 or 3.9 thats displayed on the terminal. This will be required in the next step). for e.g. in my case it was:
lrwxr-xr-x 1 user admin 24 May 7 14:33 /usr/local/bin/python -> /usr/local/bin/python3.9
Now run the below command on your terminal:
ln -s -f /usr/local/bin/python3.9 /usr/local/bin/python
(where 3.9 is the version displayed on your terminal with the previous command)
Its DONE !
To test your default version of python:
close the current terminal or start a new terminal and
run the below command :
python --version
Happy Coding!
I'm not sure if this is available on OS X, but on linux I would make use of the module command. See here.
Set up the modulefile correctly, then add something like this to your rc file (e.g. ~/.bashrc):
module load python3.3
This will make it so that your paths get switched around as required when you log in without impacting any system defaults.
For me the solution was using PyCharm and setting the default python version to the the one that i need to work with.
install PyCharm and go to file ==> preferences for new project, then choose the interpreter you want for your projects, in this case python 3.3
If you use macports, you do not need to play with aliases or environment variables, just use the method macports already offers, explained by this Q&A:
How to: Macports select python
TL;DR:
sudo port select --set python python27
If you are using a virtualenvwrapper, you can just locate it using which virtualenvwrapper.sh, then open it using vim or any other editor then change the following
# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
Change the line VIRTUALENVWRAPPER_PYTHON="$(command \which python)" to VIRTUALENVWRAPPER_PYTHON="$(command \which python3)".
If you are using macports, that has a easier way to do:
run:
port install python37
after install, set default:
sudo port select --set python python37
sudo port select --set python3 python37
restart your cmd window, finished.
Well... It's kinda old. But still deserves a good answer.
And the good one is You Don't Wanna Touch The Default Python On Mac.
Install any Python version you need via Homebrew or whatever and use it in virtualenv. Virtualenv is often considered to be something crap-like, but it's still way, wayyyy better than changing python version system-wide (macOS is likely to protect itself from such actions) or user-wide, bash-wide... whatever. Just forget about the default Python. Using playgrounds like venv is what your OS will be most, very most grateful for.
The case is, for example, many modern Linux distributions get rid of Python2 installed out-of-the-box, leaving only Python3 in the system. But everytime you try to install something old with python2 as a dependency... hope you understand what I mean. A good developer doesn't care. Good developers create clean playgrounds with python version they desire.

Categories

Resources