How to run Python2 after Homebrew Update? - python

I used to have python and python3 installed. So I could decide which python to use. I now updated python with homebrew and it migrated python3 to python. How do I run python2 now?
When I check the installation I get:
$ python -V
Python 3.6.4
$ python2 -V
-bash: python2: command not found
$ python3 -V
Python 3.6.4
If I try to simply reinstall python2 I get:
$ brew install python2
Warning: python#2 2.7.14_1 is already installed
However I cannot use python2.
python2: command not found

You're missing the symbolic link that Homebrew makes from the Cellar to the actual bin directory on your path.
Use:
brew link python2
to fix that.
You may run into a warning:
Warning: python#2 is keg-only and must be linked with --force Note that doing so can interfere with building software.
See the accepted answer to this SO question for some details on that.
In most case, you can then safely use
brew link --force python2
if you're not planning building your own libraries that require the source code (libpython.so and Python.h) for Python 2.
If you do require the source code, you'll need to provide the include paths and library paths to e.g. /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/include/python2.7 and /usr/local/Cellar/python/2.7.14_3/Frameworks/Python.framework/Versions/2.7/lib/. But that's a different topic or question.
To use pip for Homebrew's Python 2, best to use is
python2 -m pip <command>
Then you can clearly see what Python your pip command goes with, and keep it apart from Python 3 (which would be python3 -m pip).
Note
If you have Homebrew problems, first cause of action is to run
brew doctor
The error messages are usually quite helpful to fix at some of the problems.

It seems that Homebrew has changed things again - as I had both python 2.7.x and 3.x.x installed through Homebrew for the same reasons. After the initial update, python2 would no longer work, but if you used python#2 you would get access as before.
Now however, they have seemed to partially revert some of these changes. Calling python now points to the keg-only 2.7.x homebrew installation instead of the 3.x.x installation. In addition, python#2 no longer works, but python2 no does. python3 still points to the 3.x.x installation as before.
[NOTE: I have not modified my ~/.bash_profile for any of these changes to occur.]

Related

After brew install of python#3.9 "python" is no longer found

python3 is available after:
brew install python#3.9
But there is no python to be found. Not in /usr/local/bin or in /usr/local/Cellar/python#3.9/3.9.16/bin. There are many programs that look for plain old python so what is supposed to be done here?
open your terminal and type:
python3 --version
just to be sure that python is installled. In many modern operating systems, python refers to Python 2.x, which is an outdated version of Python that is no longer supported. So you should either use python3 command or to create alias for python to point to python3:
alias python='python3'

I am unable to install packages for python2 specifically, pip and pip3 point to the same version

Context: Running an exploit vs a vulnerable VM as a part of my OSCP studies. I know this VM is vulnerable to this exploit because I ran the exploit inside MSF(pentesting framework) and it worked, but doing it manually I am having dependency issues.
Setup: I am on kali, latest quarterly release
Exploit: https://github.com/andyacer/ms08_067
Pip versions output
Trying to install dependencies
Keep in mind on kali "python" points to python2.7.18, and python3 points to python3.xwhatever because of backwards compatibility (funny huh) because tons of exploits are written in python2
the script uses #!/usr/bin/env python thus points to python2.7.18
I have already tried various solutions from various SO threads as well as articles on google.
Per the Kali forums:
https://forums.kali.org/showthread.php?48570-New-Kali-build-cannot-install-pip-for-python2-7
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py
Note that I had to modify the link to pip2.7 as the old link in the forum pointed to 3.6
That should get you going.
Can you please check under /usr/local/lib that you have some version of python2 installed?
You should also be able to run python2 -V to verify that you do have python2 installed.
To install pip for python2, download get-pip.py from here and then run this command:
sudo python2 get-pip.py
This should create a pip2 symlink on your system. It might replace the existing pip symlink too. Either way, it should solve your issue.

Python not showing correct version on Mac OSX

I am trying to update Python to 3.7.4 on my Mac. Currently when I type python3 --version, it shows that I have Python 3.7.3 I have tried to update using just the standalone installer from the website as well as homebrew but even through all of these methods, python3 --version still returns to me 3.7.3. Even when I try to upgrade with Brew, it says that 3.7.4 is installed...
Picture for reference:
Please help and thank you in advance!
Update: Check what python version you are using by running which python, at this point you might figure out what version of Python is being used to solve this problem. Otherwise, follow up this steps:
I would remove all Python installations and use Homebrew to install it.
First, run which python:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Delete the entire Python.framework directory from /Library/Frameworks.
Second, run which python3:
/usr/local/bin/python3
Again, delete the entire python3 directory.
Now use brew doctor in order to see possible issues with symlinks. If you have issues run brew prune or brew cleanup --prune. This will remove all of the symlinks.
Reinstall python and python3 via homebrew:
brew install python
Some older apps may count on the Python2.7 framework being installed. It sounds like you may not have your bash $PATH set to look for homebrew binaries before the built-in ones. You can edit your ~/.profile file, and make sure that /usr/local/bin is the first entry by adding the following line to the end of .profile:
export PATH=/usr/local/bin:$PATH
ln -s -f /usr/local/bin/python /usr/local/bin/python
https://dev.to/malwarebo/how-to-set-python3-as-a-default-python-version-on-mac-4jjf
Just type in the below and it'll work
python3 --version

Which python am I using?

Having trouble making sure I'm actually using the latest versions of Python even though they are already installed via homebrew.
$ brew upgrade python3
Error: python3 3.6.2 already installed
but:
$ python3 --version
Python 3.6.0
same goes for python2:
$ brew upgrade python
Error: python2 2.7.13_1 already installed
$ python --version
Python 2.7.10`
❯ echo `which python`
If this doesn't print '/usr/local/bin/python' (where brew actually installs binaries), then there's something wrong with your $PATH (probably '/usr/local/bin' is not there or it's inserted after '/usr/bin', so the system default python is being run instead).
++ it seems that brew names its python2.7 as python2 by default, and not as python, so you may also need to create a python2->python link in /usr/local/bin directory.
Do you have separate environment variables set up for each?
I have found in the past that having multiple versions of python 2 for example, without all the environment variables, can get quite confusing!
You may find when typing python3, windows is only looking at python 3.0, unless you are in the python 3.6.2 directory.
First I modified my $PATH: sudo nano /etc/paths so that /Library/Frameworks/Python.framework/Versions/3.6/bin was not being invoked. I made sure my paths were in the right order so that python looked for /usr/local/bin/python3 and /usr/local/bin/python2 first to force the issue.
However, $ python3 --version still returned
Python 3.6.0, though brew says python3 3.6.2 already installed.
brew doctor to the rescue: homebrew recommended a couple things. python was incorrectly symlinked so I ran brew --overwrite python3. Finally, it diagnosed python coming from /Library/Frameworks/Python.framework/Versions/3.6/bin, so I sudo rm -rf /Library/Frameworks/Python.framework. Now python versions are correct.
Homebrew brew doctor warning about /Library/Frameworks/Python.framework, even with brew's Python installed
Thank you everyone for your help.

Coexistence of Homebrew and pyenv on MacOSX Yosemite

Several newbie questions about coexistence of Homebrew and pyenv on MacOSX Yosemite. These are the things that I am still confused about after applying my level of google-fu to the subject. Sorry for the length, but I wanted to be as clear as possible.
First, some background. I used brew to install pyenv, then pyenv to install python2 and python3.
brew install pyenv
brew install pyenv-virtualenv
*I also added the necessary hooks into my profiles.*
pyenv install 2.7.10
pyenv install 3.4.3
pyenv rehash
pyenv global 2.7.10
??? Question (or clarification). If brew is using a python installed by pyenv, I assume that this is frowned on because brewed packages would be built with a dependency on something outside of brew (e.g. using pyenv to uninstall a python version could break brewed packages)? It appears that brew uses whichever python comes first in the PATH (currently pyenv's version for me)? However, in each of the below cases the shims are for “python” rather than “python3” (even for pyenv 3.4.3). Am I correct then in assuming that setting “pyenv shell 3.4.3” will make brew link python commands (that it assumes are for python2.x) to pyenv’s python 3.4.3, which I guess would tend to mess things up?
pyenv shell 2.7.10
brew --config
Python: /Users/anonymous/.pyenv/shims/python => /Users/anonymous/.pyenv/versions/2.7.10/bin/python2.7
pyenv shell 3.4.3
brew --config
Python: /Users/anonymous/.pyenv/shims/python => /Users/anonymous/.pyenv/versions/3.4.3/bin/python3.4
pyenv shell system
brew --config
Python: /Users/anonymous/.pyenv/shims/python => /usr/bin/python
??? Question. If using pyenv’s pythons to brew with is a bad idea, then should I also install brewed versions of python and python3, or would this conflict and cause problems in some way? My options seem to be either use the system python that came installed on my Mac, or use brew’s own pythons for all my brewing needs. In the first case, I could just make sure to call “pyenv shell system” before any brewing, or set some alias to automate this as others have suggested (alias brew="env PATH=${PATH//$(pyenv root)/shims:/} brew"). However, this means I have to use the rather outdated system python. If instead I were to also use brew to install python2 and python3, would this conflict with versions installed using pyenv in any way, or will brew’s pythons live happily completely separate from pyenv’s (I think this is true, but I want to double check before I try it)? Would this give me a setup where all my brewing used brew’s python or python3 (I assume I still need an alias such as that above to make sure pyenv’s versions are not found first in my PATH), and everything else outside of brew would use pyenv’s pythons? Is this a bad idea for any reason?
pyenv global system
*Add to profile:*
alias brew="env PATH=${PATH//$(pyenv root)\/shims:/} brew"
brew install python
brew install python3
??? For example, consider the scenario where I want to use brew to install boost-python with python3 bindings. Does this require a brewed python3 version, and will it subsequently work for a different pyenv version of python 3.x? If I only have pyenv’s versions installed, does the --with-python3 flag even work in this case? On the other hand, if I have brew’s python3 installed in addition to pyenv’s, I assume --with-python3 will link against brew’s python3 (with above alias). If so, will boost-python even be usable when called from a python session running pyenv’s version (not sure if this is more appropriate as a question about boost-python itself), or is this completely fine?
brew install boost
brew install boost-python --with-python3
??? Opinions regarding symlinks for brew that point to pyenv python's? Another post suggested symlinking pyenv's pythons so brew can find them. Besides the obvious caveat that brew now depends on python's that it does not itself manage, is there anything horribly wrong with this idea?
ln -s $(brew --cellar) ~/.pyenv/versions
I'll try to answer each of your questions.
1 Does brew "frown" upon using pyenv's Python rather than brew's?
In short, yes. They lay out the whole story here, and in short you're on your own to manage issue that come up.
2 Will brew Python and pyenv Python conflict?
No. As Tim Smith says in another answer, "nothing bad should happen if you do". Use your PATH to manage which Python you (or an application) sees (and this is what pyenv does).
3 Packages with Python dependency satisfied by brew Python working with "oustide" Python from pyenv
As Tim said, this shouldn't be a problem.
4 Use symlinks so that we only use pyenv Python?
I think you're referring to this post, and it's a fun idea, but likely perilous for maintenance. Fwiw, I'm doing this now.
I lay out how to do it here.
A clarification: Homebrew uses system Python whenever it doesn't make a difference exactly which Python 2.7 it uses (which is pretty common). If you build something --with-python, that will use the first Python in PATH, if that Python is Python 2.7. Otherwise, Homebrew will install and use Homebrew python.
I would not expect that you will find it necessary to install Homebrew's python and python3 but nothing bad should happen if you do. Note that if you use pip or easy_install to install a package that installs scripts (like ipython), those scripts are written so that the package will always be invoked with the python against which it was installed. (You can always get around this behavior by running packages like python -m ipython.) Otherwise, you can control which python you're using just by setting $PATH.
Building boost-python against whatever python 3.4 will work with any other python 3.4. It will probably not work with python 3.5. (This used to be more complicated, but it isn't, anymore!)
Symlinking your pyenv pythons into /usr/local shouldn't hurt but doesn't do anything except put them in $PATH; it won't help Homebrew find them otherwise.

Categories