Python installation mess on Mac OS X, cannot run python - python

I stupidly downloaded python 3.2.2 and since then writing 'python' in the terminal yields 'command not found'. Also, when starting the terminal I get this:
Last login: Wed May 2 23:17:28 on ttys001
-bash: export: `folder]:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/usr/local/git/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/android-sdk-mac_86/tools:/Applications/android-sdk-mac_86/platform-tools:/usr/local/git/bin:/usr/X11/bin:/usr/local/ant/bin': not a valid identifier
Why the Android SDK folder is there is beyond me. It's all jazzed up. Any ideas how can I remove the offending file, folder or fix his problem? I've checked the System Profiler and python 2.6.1 and 2.7.2.5 shows up.

If the binary was not overridden it's typically located in
/System/Library/Frameworks/Python.framework/Versions/X.X/bin/python
where X.X is your version (probably 2.7) if it's not located there it could be sometimes found in
/Library/Frameworks/Python.framework/Versions/X.X/bin/python
If you find the right binary and can run it, go ahead and start symlinking it where you need to and making the necessary patches. My first piece of advice would be changing your Current to point to the old version.
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/X.X /System/Library/Frameworks/Python.framework/Versions/Current
or
sudo ln -s /Library/Frameworks/Python.framework/Versions/X.X /Library/Frameworks/Python.framework/Versions/Current

Something got messed up in your $PATH. Have a look in ~/.profile, ~/.bashrc, ~/.bash_profile, etc., and look for a line starting with export that doesn't end cleanly.

A very ugly solution to your profile problem since $PATH seems to be very wrong (as was mentioned by blahdiblah)
/bin/cp ~/.profile /tmp/bkup_profile
/bin/cat /tmp/bkup_profile | /usr/bin/grep -v "PATH" > ~/.profile
Open a new terminal and see if you can run commands normally try ls, echo, sudo for example.

Related

How to insert the Homebrew directory at the top of your PATH environment variable?

I need little help here. For your information, I'm a very new to Python, and just trying to tinker with it. So, I have this website 'The hitchhiker's guide to python'. Now, I'm on a section of 'Installing Python 3 on Mac OS X'. Here is the link -> http://docs.python-guide.org/en/latest/starting/install3/osx/#pip
Anyway I wanted to do it right, so freshly formatted my Mac, and Xcode as well. And I followed the instruction which says 'xcode-select --install' . After that, I started install homebrew within the terminal $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
I guess I coped with this until now, but here is the problem occurs.
He says
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
So, with some searches, I edited my path in nano. by following
sudo nano /etc/paths
/usr/local/bin
/usr/local/sbin <<<--When I opened the paths, there wasn't this 'usr/local/sbin' So I typed it.
/usr/bin
/bin
/usr/sbin/
/sbin
When I opened the path, there was no '/usr/local/sbin'. so I typed it.
And I quit the terminal, echo the path out, nothing happend.
If I do which python it shows me /usr/bin/python.
I do have python3 installed, But when I do python -V, it's still
Python 2.7.10
How can I insert the Homebrew directory at the top of your PATH environment variable?
By means, How can I set my mac to set python3 to default???
Appreciate it in advance!
You misunderstood the instructions about the PATH. PATH is an environment variable which can be set in the .profile file in your home directory (create it if it doesn't exist), and you need to copy exactly what the page said to the bottom of the file:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
Once this is done, restart your Terminal and you should be able to use the python3 command.
Go to your terminal and run:
$ which brew
/usr/local/bin/brew
you will get your brew path and make sure you add this path to ~/.profile PATH like this:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
Then run this to make changes effective:
$ source ~/.profile
The file in /etc/ is only run at startup, and is global over all users. I suggest you use the files in your home directory (.bash_profile or .zshrc depending on your shell). These files are read every time a terminal is opened.
Add this line:
export PATH=/usr/local/sbin:$PATH
To update an existing terminal, you can update the settings by
> source ~/.bash_profile

Virtualenv uses wrong python, even though it is first in $PATH

I had a problem where python was not finding modules installed by pip while in the virtualenv.
I have narrowed it down, and found that when I call python when my virtualenv in activated, it still reaches out to /usr/bin/python instead of /home/liam/dev/.virtualenvs/noots/bin/python.
When I use which python in the virtualenv I get:
/home/liam/dev/.virtualenvs/noots/bin/python
When I look up my $PATH variable in the virtualenv I get:
bash: /home/liam/dev/.virtualenvs/noots/bin:/home/liam/bin:/home/liam/.local/bin:/home/liam/bin:/home/liam/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin: No such file or directory
and yet when I actually run python it goes to /usr/bin/python
To make things more confusing to me, if I run python3.5 it grabs python3.5 from the correct directory (i.e. /home/liam/dev/.virtualenvs/noots/bin/python3.5)
I have not touched /home/liam/dev/.virtualenvs/noots/bin/ in anyway. python and python3.5 are still both linked to python3 in that directory. Traversing to /home/liam/dev/.virtualenvs/noots/bin/ and running ./python, ./python3 or ./python3.5 all work normally.
I am using virtualenvwrapper if that makes a difference, however the problem seemed to occur recently, long after install virtualenv and virtualenvwrapper
My problem was that i recently moved my project with virtualenv to another location, due to this activate script had wrong VIRTUAL_ENV path.
$ cat path_to_your_env/bin/activate
... # some declarations
VIRTUAL_ENV="/path_to_your_env/bin/python" # <-- THIS LINE
export VIRTUAL_ENV
... # some declarations
To fix this, just update VIRTUAL_ENV in activate script.
Also you maybe need to fix first line of your bin/pip to link to real python path.
As tdelaney suggested in the comments, I ran alias and found that I had previously aliased python to /usr/bin/python3.5 in my .bashrc.
I removed that alias from my .bashrc, ran unalias python, and source ~/.bashrc and the problem was solved.
If you don't get the program that which says you should get, you need to look higher up the chain than the platform executor. Shells typically have a way to alias commands and on most unixy shells you can just enter alias to see which commands have been remapped. Then its just a matter of going to the config files for your shell and removing the alias.
Sometimes people alias python to try to sort out which python they should be using. But there are usually other, better ways. On my linux machine, for example, python3 is in the path but is a symlink to the real python I am using.
td#mintyfresh ~ $ which python3
/usr/bin/python3
td#mintyfresh ~ $ ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Feb 17 2016 /usr/bin/python3 -> python3.4
td#mintyfresh ~ $
This is nice because non-shell programs running python get the same one I do and virtual environments work naturally.
On Cygwin, I still have a problem even after I created symlink to point /usr/bin/python to F:\Python27\python.exe. Here, after source env/Scripts/activate, which python is still /usr/bin/python.
After a long time, I figured out a solution. Instead of using virtualenv env, you have to use virtualenv -p F:\Python27\python.exe env even though you have created a symlink.
I'm currently having the same problem. Virtualenv was created in Windows, now I'm trying to run it from WSL.
In virtualenv I renamed python.exe to python3.exe(as I have only python3 command in WSL). In $PATH my virtualenv folder is first, there is no alias for python. I receive which python3
/usr/bin/python3. In /usr/bin/python3 there is symlink `python3 -> python3.6. I suppose it doesn't matter for order resolution.
Had the exact same problem.
I ran:
virtualenv -p /venv/bin/python3 env
and got a permission denied.
so i tried:
sudo chmod 777 -R /venv/bin
which python and print(sys.executable)
were not agreeing for me. This meant that with an active virtualenv pip install <package> would install to the virtualenv, but running python would be the base install.
I eventually got around this by running
virtualenv -p \path\to\python.exe --always-copy <venvName>
I'm not sure if specifying the path to the original python is really necessary, but can't hurt. According to the man page:
--copies, --always-copy try to use copies rather than symlinks, even when symlinks are the default for the platform (default: False)
I'm using windows powershell with msys64.

Install Python 3 to /usr/bin/ on macOS

I installed python2.x and python3.x using homebrew and the executable python paths are listed below:
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
It's quite too long and not so clean to write a shebang in a python code to make it runnable on Terminal:
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python OR
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
I prefer
#!/usr/bin/python OR
#!/usr/bin/python3
My issue here is, how can I correcly move or reinstall python on macOS to /usr/bin such as /usr/bin/python OR /usr/bin/python3 Instead of
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
This is NOT possible on Mac OS X El Capitan anymore as from then on System Integrity Protection prevents that. More info in Cannot create a symlink inside of /usr/bin even as sudo
Create a symbolic link in /usr/bin/
Open terminal and do:
$ sudo ln /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python
$ sudo ln /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 /usr/bin/python3
You can now do what you wanted to do.
Edit: Unfortunately as you can read from the other answers, this solution no longer works on MacOS >= El Capitan due to System Integrity Protection. (See here)
A possible alternative is to use the folder /usr/local/bin that should be accessible.
The basic question in the OP seems to not be doable because the newer Mac OSes have "System Integrity Protection" which prevents "unauthorized" changes to key directories such as /usr/bin, and sudo cannot override that.
The suggestion of using /usr/local/bin and /usr/local/Frameworks seems like it should work (I haven't tried it). However, at https://opensource.com/article/19/5/python-3-default-mac, Matthew Broberg suggests that it is likely to create problems when updating.
In that same article, Moshe Zadka recommends using pyenv to manage Python environments and using shell aliases rather than symlinks.
I was going to go with the /usr/local suggestion above, but having found this contraindication, I'm going to try Moshe's method. I'll report back here if I hit any snags.
Update: I followed the method and I still was getting pip not found and which python and python -V were not giving me the expected results. Doh! I ran:
. ~/.bash_profile
(where I had put the recommended eval "$(pyenv init -)" command) and suddenly everything was finding the paths and versions as expected.

Error Installing OpenCV with Python on OS X

I have been trying to install Open CV 3 on my mac using this tutorial but I cannot get past step three.
So after I do
brew install python
I do
nano ~/.bash_profile
And the at the bottom of the script I paste
# Homebrew
export PATH=/usr/local/bin:$PATH
After that I reload the file like this
source ~/.bash_profile
Finally I check the python like this
which python
And it prints
/usr/bin/python
instead of
/usr/local/bin/python
I have also tried edited the file in TextEdit but it has the same result.
Am I doing something wrong or is this just a bad tutorial?
Thank You in Advance!
Edit:
# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
##
# Your previous /Users/UserName/.bash_profile file was backed up as /Users/UserName/.bash_profile.macports-saved_2016-07-26_at_12:50:19
##
# MacPorts Installer addition on 2016-07-26_at_12:50:19: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
# Homebrew
export PATH=/usr/local/bin:$PATH
pydoc3.5
python3
python3-32
python3-config
python3.5
python3.5-32
python3.5-config
python3.5m
python3.5m-config
Is there a
/usr/local/Cellar/python/2.7.12/
directory? (Version number might differ.)
Is there a
/usr/local/bin/python
file?
If the Cellar directory is present, but the file isn't, then Homebrew decided to be careful and not put Python in /usr/local/bin/ immediately.
You could manually do
brew link python
and see if there's now a
/usr/local/bin/python
file.
In your case, it appears you have some files related to Python (they might be from a Python 3 installation, can't tell), such as 2to3. You can safely overwrite them, since Python 2 also has this.
Thus:
brew link --overwrite python
is fine.
Note:
Specific Python versions will always exist as python2.7, python3.5 etc (including the full path as necessary). Thus, even overwriting the python executable is safe (provided it's not the system one in /usr/bin): you should then simply be explicit which python executable to use.
Also, when using a tool like pip, you can make sure you're using the correct version by running it e.g. as
/usr/local/bin/pythnon2.7 -m pip <...>
or whatever python executable you want to install things for.
Okay so one brute force solution could be this one https://stackoverflow.com/a/9821036/128517
But maybe you could check the value of your $PATH after source ~/.bash_profile
typing
> echo $PATH
and see if /usr/local/bin is indeed at the beginning.
if it's not, you might need to check if there's another export before yours or maybe you need to edit .profile instead.

Mamp Changed My Python Path

To get straight to the point. MAMP decided to change my python path, for some reason I do not understand. I only noticed this after 20 minutes of annoyance. But that is not my question. My question is how to change this back to my default python install path.
Chriss-MacBook-Pro:~ chrisburgin$ which python
/Applications/MAMP/Library/bin/python
I had to comment out my MAMP stuff inside my .zshrc and then $ source ~/.zshrc and then when I typed $ which python it now pointed to: /usr/bin/python

Categories