Trouble Installing Python with Framework-style Build (OS X) - python

In attempting to get started learning and developing python, I've tried to follow the Python Guide to installing python on OS X, but haven't found it particularly "noob friendly." I have a new MacBook (Mtn. Lion - OS X 10.8.3) wich comes with Python 2.7.2 built in. But the guide advises installing a "framework-style build" via homebrew. So:
I installed homebrew via ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
Then it tells you to add homebrew to the top of your PATH by adding it to your ~/.bashrc file. ls -a showed that I did not have a ~/.bashrc file in my home directory. After searching Stack Overflow on how to do that, I ran nano ~/.bashrc, and inserted the line export PATH=/usr/local/bin:$PATH to the file and saved the file.
I then ran brew install python --framework and the install completed.
Then, the guide says to "add the new Python scripts directory to your PATH" so, I'm assuming that means I need to add the line it provides to my ~/.bashrc file also. So, I added export PATH=/usr/local/share/python:$PATH to my ~/.bashrc file above my previous entry.
Finally, this is where I run into trouble, it says to easy_install pip. However, when I do that I get an error 13.
So, here are the things I need some help with.
Was I correct in my assumptions about how to add homebrew and python scripts to my PATH?
Did I do something wrong or do I just need to use sudo to install pip? (I'm really sorry if the answer is already on this page but even those answers don't make total sense to me and I want to be careful and not screw something up)
After installing the framework-style build of python (which I believe was the current 2.7.3), how come running python in my terminal still shows v2.7.2?
Thanks! I appreciate any help.

I've tried to follow the Python Guide to installing python on OS X,
but haven't found it particularly "noob friendly.
Yes, I think it is misleading/outdated.
Then it tells you to add homebrew to the top of your PATH by adding it
to your ~/.bashrc file. ls -a showed that I did not have a ~/.bashrc
file in my home directory. After searching Stack Overflow on how to do
that, I ran nano ~/.bashrc, and inserted the line export
PATH=/usr/local/bin:$PATH to the file and saved the file.
On the Mac, just use ~/.profile
I then ran brew install python --framework and the install completed.
I think you don't need the --framework option unless you want to replace your Mac OS default installation and need an Mac OS Framework-style directory layout. There is no need to replace it though, the homebrew installation will take precedence anyway.
Then, the guide says to "add the new Python scripts directory to your
PATH" so, I'm assuming that means I need to add the line it provides
to my ~/.bashrc file also. So, I added export
PATH=/usr/local/share/python:$PATH to my ~/.bashrc file above my
previous entry.
Again, do it in ~/.profile. And don't forget to do a
source ~/.profile
otherwise the changes will only become active in any new terminal window, not the one you are currently using.
Finally, this is where I run into trouble, it says to easy_install
pip. However, when I do that I get an error 13.
The error shows that you try to install it your Mac OS system's default Python library (rather than in /usr/local, homebrew style), which would require root privileges. Just don't.
Also, with homebrew python, pip is already installed.
Check your path:
$ which pip
/usr/local/bin/pip
$ ls -l /usr/local/bin/pip
[..] /usr/local/bin/pip -> ../Cellar/python/2.7.3/bin/pip
Added bonus: Then do
pip install virtualenv
and use that.
And to your questions:
Was I correct in my assumptions about how to add homebrew and python
scripts to my PATH?
Yes, but use .profile and do a source .profile afterwards.
Did I do something wrong or do I just need to use sudo to install pip?
(I'm really sorry if the answer is already on this page but even those
answers don't make total sense to me and I want to be careful and not
screw something up)
You don't need sudo with homebrew, and pip is installed automatically with homebrew python.
After installing the framework-style build of python (which I believe
was the current 2.7.3), how come running python in my terminal still
shows v2.7.2?
Probably PATH not correct, do echo $PATH and check that it is correct. That is unrelated to being "framework-style" or not, though.

If you installed python with homebrew, you should already have pip installed. Try running
pip --version
to see whether and where pip is installed. Hopefully it's in a /usr/local/... path where your other homebrew things live.
Also before you install too much more with homebrew be sure to run these commands:
brew update
brew doctor
Good luck!

Related

Can not run Ansible after installing python 3.10

Suddenly I cannot run ansible.
Whenever I try any ansible command like ansible-playbook, ansible version, etc. it shows error
`/Users/myusername/Library/Python/2.7/bin/ansible: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/: bad interpreter: No such file or directory`,
Even if I let ansible run different python version, like ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3, it shows the same error.
FYI: which ansible returns /Users/myusername/Library/Python/2.7/bin/ansible
I guess it is related to my recent installation of python. Since I installed a python3.10 recently, The python2.7 becomes not work. Note I did not remove anything about python2.7 myself. looks like the installation of python3.10 changed python2.7 setting.
For most other application, I now pointed the system to use python3 as workaround, e.g. I set CLOUDSDK_PYTHON=/usr/bin/python3 to make accessing cloud cli works again. But for Ansible, I have not figured out how to make it work. I am using MacOS terminal.
Does anybody know how to resolve the above issue so that I can run ansible again? Anyway is OK as long as I can run ansible. Either reinstall ansible and let it use python3 instead of using Python2.7 or guide me how to reinstall python2.7 (in a right way, not messy with python3.10, currently I am scared of installing python, I am afraid if I reinstall python2.7, both python3.10 and 2.7 will be out of work then I cannot work).
Please help. Thanks.
You will have to do some cleanup on your system because somehow, Python 2.7 was removed from you system. This might have happened due to updating to macos 12.4 Monterey at some point, because Monterey 12.3 and newer removed the system-provided Python 2.7, replacing it with a Python 3.8.9 installation (/usr/bin/python3).
However, you still have stuff in your environment that reference all those Python 2.7 things, like your ${HOME}/Library/Python/2.7/bin/ansible directory.
Here are the things you can do to (hopefully) make ansible and your environment work again.
Change your shell's PATH environment.
You're probably using zsh since it's the default shell on macos. Have you ever changed your .zshrc or other environment files to add /Users/<name>/Library/Python/2.7/bin in your PATH? You will need to remove that.
Additionally, if you strictly want to use the Python 3.10 you manually installed (and not Monterey's system-provided /usr/bin/python3 which is python v3.8.9), you will probably need a PATH that looks like this...
# somewhere in your ~/.zshrc, probably near the bottom
export PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
Re-install Ansible
With whatever python3 binary you're using, re-install ansible
python3 -m pip install --user ansible
This will end up installing ansible into ${HOME}/Library/Python/<VERSION>/bin
Update PATH again to include new bin dir
Building on part (1) above, you want to include the bin directory for Python stuff in your user directory, to be able to refer to anything installed by pip install --user.
# somewhere in your ~/.zshrc, probably near the bottom
export PATH="${HOME}/Library/Python/3.10/bin:${PATH}"
export PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
Reload your shell and try to run ansible
# if you already have a shell open, run this
# to reload your zsh configs
exec zsh
# hopefully this returns the correct path!
which ansible
# and hopefully this runs!
ansible --help
Reinstall Anything Else You Need
You reference stuff like CLOUDSDK_PYTHON which sounds like you also have stuff like the gcloud module installed. Time to reinstall those with your new Python.
python3 -m pip install --user gcloud
# and whatever else needs reinstalling
Hopefully this all fixes your environment. Now you can clean up the remnants of Python 2.7 stuff from your home directory once everything else is working, as this directory has broken module installs that reference a deleted system Python anyway.
cd ~/Library/Python
rm -rf 2.7

Homebrew not linking python correctly?

I installed both python 2.7.13 and python 3.6.2 with homebrew and updated my ~/.bash_profile as such:
# Homebrew
export PATH=/usr/local/bin:$PATH
Python3 was linked fine. However, "which python" would still give me
/usr/bin/python
while "which python2" produces
/usr/local/bin/python2
It looks like homebrew installed python 2 as "python2" and never linked "python" to the new installation. This is causing me a lot of trouble down the road when installing virtualenvwrapper etc.
By the way, I also did
brew link python.
Anybody know why this is and how to fix it?
Much appreciated!!
They change that behavior here
Today I’d like to announce Homebrew 1.3.0. The most significant change
since 1.2.0 is that brew install python no longer installs a python
binary without manual PATH additions and instead installs a python2
binary. This avoids overriding the system python binary by default
when installing Python as a dependency. It also paves the way to
eventually have python be Python 3.x.
You will have to symlink python to the version of python installed by homebrew that you want.
You can do:
$ln -s /usr/local/bin/python2 /usr/local/bin/python
To symlink python to the homebrew Python2.x installation or do:
$ln -s /usr/local/bin/python3 /usr/local/bin/python
to link it to the Python 3.x hombrew installation.
gsi-frank's solution solves the problem quite well but after using his solution, you might encounter problems with your pip3. Your pip3 might be linked to the old instance and therefore unusable for your new python instance.
Type which pip3 to see where your pip3's path.
If your path is linked to your new instance, then ignore the rest of this answer.
Type echo $PATH to see your current path. If it contains your old python instance's path then you will need to remove it from your .bash_profile
To solve this: check your .bash_profile and see if there is a path to your old python instance.
If the old path is in there, you can delete it.
I had this problem and here's a link to the question if needed:
How to change pip3 path after installing python with homebrew?
To find your .bash_profile:
Go to your Finder
Right click and click Go to a folder... and type ~
Press CMD + shift + . to see hidden files
Right click your .bash_profile and open in a text editor

Unable to Run Python 3 After Homebrew Installation

After installing Homebrew using the script on their homepage and checking if everything was alright with brew doctor, I issued brew install python3 in order to install Python 3 on my Mac.
Everything seemed fine until I tried running python3 --version; I ended up getting:
-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory
I checked in the file directory to see what was going on and indeed, I didn't see any files pertaining to Python in my framework folder. It also looks like Python 2.7 isn't on my Mac either.
This is what I got after installing Python 3:
Summary
🍺 /usr/local/Cellar/python3/3.5.1: 3,438 files, 51.5M
edit_2: maybe this has something to do that there is no Python framework? I just read this off the Python website:
The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python, respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.
I think I detected what the problem is.
I guess that, at a certain moment, you had installed python from the official site instead of via Homebrew.
In my case, I installed it via the official website Python 3.6.4. A few months later, I wanted to upgrade it and noticed that it was very complex. So, I decided to move to Homebrew. Open a terminal window and let's try to fix this:
First, let's uninstall previous Python versions:
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /usr/local/bin/python3
Then, remove the previous frameworks from the $PATHvariable:
nano ~/.bash_profile
You will see something like that:
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH`
This is the problem: These paths don't exist. Comment the $PATH editions (or erase them):
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
# export PATH
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
# export PATH
Restart the computer and install via Homebrew Python 2 and 3:
brew update
brew install python
brew install python3
This worked for me. Now, if type python3 --version I get Python 3.7.0, and everything works fine :)
I had the same issue. I learned how to fix it for good:
Open "Applications" in Mac Finder and drag Python to the trash bin.
Empty the trash bin
If you have an error as above, then an official Python installation has been performed (as others have mentioned) via e.g. Python.org. This creates some kind of alias for the python or python3 commands outside a Bash alias. So while the command where python3 may point to /usr/local/bin/python3, python3 will still try to call /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.
Note:
the MacOS system Python is /usr/bin/python
Homebrew Python(s) will be located in /usr/local/bin/
Pythons installed as an Apple application live in /Library/Frameworks/Python.framework/
Okay, this is what I gathered:
Don't delete the Python framework!
If it's deleted, then python3 --version won't work
Just install Python from the Python website
The framework will return and python3 --version will work
This error:
-bash: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3: No such file or directory
suggests a remnant of some previous (attempt at an) installation of Python 3 using a different way (not Homebrew).
(I think this is actually where the Python installation from www.python.org goes. I wouldn't know though, as I've either never tried that package, but only installed the www.python.org version from source. This would suggest, though, that you already had an attempt at installing Python 3.5, something failed, and you're now trying Homebrew instead.)
I'd suggest moving (renaming) this out of the way, so your system doesn't pick it up. Something like
mv /Library/Frameworks/Python.framework/Versions/3.5 /Library/Frameworks/Python.framework/Versions/3.5-aside
(if there other versions of Python 3 in that directory, you may want to do the same for those.)
Also check that python3 isn't an alias. Commands such as
which python3
type python3
alias python3
will reveal that.
With the interfering Python 3 out of the way, try re-installing Python 3 through homebrew again. You may have to do an uninstall + reinstall.
Read carefully any homebrew messages once the installation is done, in particular if it mentions something about linking files: you may need to run something like brew link python3.

Python easy_install in a virtualenv gives setuptools error

There are a number of other StackOverflow questions similar to this one, but in each case, the platform was different or the error message was different or the solution had no effect or was outdated. I am trying to set up a Python 2.7.6 virtualenv and install modules into it but easy_install gives me errors indicating that setuptools is not available. But AFAIK easy_install is part of setuptools, so this makes no sense.
The problem only happens in a virtualenv. Here's what I've done:
Created a brand new Red Hat 5 virtual machine
Did a yum -y update to get the latest stuff, rebooted
Downloaded Python-2.7.6.tar.gz, unzipped, ./configure; make; sudo make install
Confirmed that python -V gives me 2.7.6 and sudo python -V also gives me 2.7.6
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
Modified ez_setup.py to add the --no-check-certificate flag to wget to get around proxy server problems in our network
sudo python ez_setup.py
sudo easy_install pip
sudo pip install virtualenv
virtualenv virtpy
. virtpy/bin/activate
easy_install elementtree
All of these steps succeed except for the last one, which fails with:
Traceback (most recent call last):
File "/home/gperrow/virtpy/bin/easy_install", line 7, in <module>
from setuptools.command.easy_install import main
File "/home/gperrow/virtpy/lib/python2.7/site-packages/setuptools/command/easy_install.py", line 44, in <module>
from setuptools.package_index import PackageIndex
File "/home/gperrow/virtpy/lib/python2.7/site-packages/setuptools/package_index.py", line 203, in <module>
sys.version[:3], require('setuptools')[0].version
File "/usr/local/bin/scripts/pkg_resources.py", line 584, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/local/bin/scripts/pkg_resources.py", line 482, in resolve
raise DistributionNotFound(req) # XXX put more info here
pkg_resources.DistributionNotFound: setuptools
I'm starting with a clean VM and I've done nothing really unusual but I'm finding "easy_install" anything but. Am I doing something wrong or am I missing one or more steps?
I can't tell why exactly you get errors, but I am confident that there is a systematic approach that lets you cleanly install your custom Python including a working pip and virtualenv. In the following, I describe the procedure that I would use.
First of all, leave your system's Python untouched, for a number of reasons. One of them is that parts of your Linux distribution might depend on the specifics of its default Python. You don't want to break these parts. Another reason is that the vanilla Python installed to default locations might become confused by residuals of the original Python (distributions may have a specific Python/dist-packages/site-packages directory layout that differs from the vanilla one). This might or might not be a real problem in practice -- you can conceptually prevent these issues by not overwriting the system's Python. Another argument is that there is no need to install Python 2.7.6 as root. Install it as unprivileged user (called 'joe' from here on) and put it into /opt or something. This would be a clean start.
After having set up your custom Python, create yourself a little shell script, e.g. setup.sh that sets up the environment for using your custom Python version. Make sure to adjust and clean up the environment. Obviously, this especially affects PATH and PYTHONPATH. I would make sure that PYTHONPATH is unset and that PATH properly points to the custom install. Look at env and try to identify if there is anything left that might configure python in unexpected ways. After all, make sure that
$ command -v python
$ python -v
, executed as joe, look right.
Still being joe and under the proper environment, install pip for the custom Python. According to http://pip.readthedocs.org/en/latest/installing.html, download https://raw.github.com/pypa/pip/master/contrib/get-pip.py and execute it: python get-pip.py. Validate that it installed properly and that your environment is still right:
$ command -v pip
/CUSTOM/PYTHON/bin/pip
$ pip --version
pip 1.x.x from /CUSTOM/PYTHON/lib/python2.7/site-packages
At this point you should make sure that your environment does not contain any VIRTUALENV_* variables (which might have been set by your distribution or whatever component (unlikely, but worth checking)). If any VIRTUALENV_* variable is set, it most likely configures virtualenv in an unexpected way. Get rid of this (unset, or change). Then go ahead and install virtualenv into your new Python with the new pip, via pip install virtualenv. It might also be worth a try to install the latest development version of virtualenv via pip install https://github.com/pypa/virtualenv/tarball/develop.
Create and activate a new virtual environment. Using command -v pip, verify that pip comes from the virtual environment. Then install your custom package(s).
Note: I would definitely use pip to install things to the new virtual environment, and not easy_install, if possible. pip will quite soon be the official installer tool (it will be included with Python 3.4). If for some reason you really depend on easy_install, this should be possible (the easy_install command is provided by the virtual environment), but just to be sure you should also verify this via command -v easy_install.
I have a couple of suggestions, and also what I believe is your problem. Let's go for the problem first.
I noticed you said in your third bullet point
Confirmed that python -V gives me 2.7.6 and sudo python -V also gives me 2.7.6
But you did NOT display the python version visible after the 2nd to last bullet point, when you activate your virtualenv. Since that step plays with your path, it's possibly not invoking the python you think.
What does python -V give AFTER you activate your virtualenv? I strongly suspect after the activate step you are being redirected and invoking the system python (which on RHEL is typically <= 2.5). It's important to RHEL that you not upgrade the system installed version of python, and the folks at RedHat go through several hoops to ensure this.
My first suggestion is to go back to the step where you installed python, and specify an alternate installation. Something like:
./configure --enable-shared --prefix=/opt/python2.7 && make && sudo make install
(note: --enable-shared is not specifically required...just a good idea)
The second suggestion I have is related to python package management. We will typically use easy_install to get pip installed. Once we have pip, we switch over to using pip for everything. It would be interesting to know what happens in your last step AFTER you activate your virtualenv, you then
pip install elementtree
One more suggestion. After you've installed python2.7, and then installed virtualenv, pip & easy_install, you should then have *-2.7 version of these scripts available. It might be better to try invoking them & specify the version. This removes any ambiguity about the version you're requesting. eg:
virtualenv-2.7 virtpy
pip-2.7 install elementtree
easy_install-2.7 elementtree
Your approach is correct and other answers (Jan-Philip's and Piotr's) also, but your problem is simple:
You use a part of an old installation of setuptools on Python sys.path together with the new installation. It is obvious that the line numbers in pkg_resources.py should be by approximately by 100 lines greater for the current version of setuptools than it is in your traceback:
...
File "/usr/local/bin/scripts/pkg_resources.py", line 669, in require ## 584 is too old
needed = self.resolve(parse_requirements(requirements))
File "/usr/local/bin/scripts/pkg_resources.py", line 572, in resolve ## 482 is too old
raise DistributionNotFound(req)
The line numbers of the first three files of setuptools in traceback are correct: "virtpy/bin/easy_install", "virtpy/.../site-packages/setuptools/command/easy_install.py", "virtpy/.../site-packages/setuptools/package_index.py". Using different versions of the same package is everytimes a big problem.
Examine your Python sys.path by python -c "import sys; print sys.path" and think why "/home/gperrow/virtpy/lib/python2.7/site-packages" is not before "/usr/local/bin/scripts", or search everywhere for the string "/usr/local/bin/scripts". Fix it. One possible solution could be to install setuptools again locally into your active virtualenv: python ez_setup.py. (A fast method of examining the reason is to determine first if it is caused by user settings. Create a new user account, run the last three commands (virtualenv virtpy; ...) from that account, look at result and delete that user. If it works, examine which configuration file in your profile makes the problem.)
Verify finally that the new pkg_resources are used:
(virtpy)$ python -c "import pkg_resources; print pkg_resources"
<module 'pkg_resources' from '/home/you/virtpy/lib/python2.7/site-packages/pkg_resources.pyc'>
# not the obsoleted /usr/local/bin/scripts/...
Did you try to use Software Collections? It is a standard approach on RHEL to get more up to date packages like python27:
http://developerblog.redhat.com/2013/08/08/software-collections-quickstart/
Then to use python27 you have to prefix all your python commands with
scl enable python
e.g. to have a bash with python27:
scl enable python27 bash
This setup will possibly have a more compatible environment.

Problem with virtualenv in Mac OS X

I've installed virtualenv via pip and get this error after creating a new environment:
selenium:~ auser$ virtualenv new
New python executable in new/bin/python
ERROR: The executable new/bin/python is not functioning
ERROR: It thinks sys.prefix is u'/System/Library/Frameworks/Python.framework/ Versions/2.6' (should be '/Users/user/new')
ERROR: virtualenv is not compatible with this system or executable
In my environment:
PYTHONPATH=/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
PATH=/System/Library/Frameworks/Python.framework/Versions/2.6/bin:/Library/Frameworks/Python.framework/Versions/2.6/bin:/Library/Frameworks/Python.framework/Versions/2.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
How can I repair this?
Thanks.
Just in case there's someone still seeking for the answer.
I ran into this same problem just today and realized since I already have Anaconda installed, I should not have used pip install virtualenv to install virtual environment as this would give me the error message when trying to initiate it later. Instead, I tried conda install virtualenv then entered virtualenv env_mysite and problem solved.
Like #RyanWilcox mentioned, you might be inadvertently pointing virtualenv to the wrong Python installation. Virtualenv comes with a -p flag to let you specify which interpreter to use.
In my case,
virtualenv test_env
threw the same error as yours, while
virtualenv -p python test_env
worked perfectly.
If you call virtualenv -h, the documentation for the -p flag will tell you which python it thinks it should be using; if it looks wonky, try passing -p python. For reference, I'm on virtualenv 1.11.6.
In case anyone in the future runs into this problem - this is caused by your default Python distribution being conda. Conda has it's own virtual env set up process but if you have the conda distribution of python and still wish to use virtualenv here's how:
Find the other python distribution on your machine: ls -ls /usr/bin/python*
Take note of the availble python version that is not conda and run the code below (note for python 3 and above you have to upgrade virtualenv first): virtualenv -p python2.7(or your python version) flaskapp
I've run across this problem myself. I wrote down the instructions in a README, which I have pasted below....
I have found there are two things that work:
Make sure you're running the latest virtualenv (1.5.1, of this writting)
If you're using a non system Python as your standard Python (which python to check) Forcefully use the System supplied one.
Instead of virtualenv thing use /usr/bin/python2.6 PATH/TO/VIRTUALENV thing (or whatever which
python returned to you - this is what it did for me when I ran into this issue)
I had the same problem and as I see it now, it was caused by a messy Python installation. I have OS X installed for over a year since I bought a new laptop and I have already installed and reinstalled Python for several times using different sources (official binaries, homebrew, official binaries + hand-made adjustments as described here). Don't ask me why I did that, I'm just a miserable newbie believing everything will fix itself after being re-installed.
So, I had a number of different Pythons installed here and there as well as many hardlinks pointing at them inconsistently. Eventually I got sick of all of them and reinstalled OS X carefully cleaned the system from all the Pythons I found using find utility. Also, I have unlinked all the links pointing to whatever Python from everywhere. Then I've installed a fresh Python using homebrew, installed virtualenv and everything works as a charm now.
So, my recipe is:
sudo find / -iname "python*" > python.log
Then analyze this file, remove and unlink everything related to the version of Python you need, reinstall it (I did it with homebrew, maybe official installation will also work) and enjoy. Make sure you unlink everything python-related from /usr/bin and /usr/local/bin as well as remove all the instances of Frameworks/Python.framework/Versions/<Your.Version> in /Library and /System/Library.
It may be a dirty hack, but it worked for me. I prefer not to keep any system-wide Python libraries except pip and virtualenv and create virtual environments for all of my projects, so I do not care about removing the important libraries. If you don't want to remove everything, still try to understand whether your Pythons are, what links point to them and from where. Then think what may cause the problem and fix it.
I ran into a variation of this "not functioning" error.
I was trying to create an environment in a folder that included the path ".../Programming/Developing..." which is actually "/Users/eric/Documents/Programming:Developing/"
and got this error:
ImportError: No module named site
ERROR: The executable env/bin/python2.7 is not functioning
ERROR: It thinks sys.prefix is u'/Users/eric/Documents/Programming:Developing/heroku' (should be u'/Users/eric/Documents/Programming:Developing/heroku/env')
ERROR: virtualenv is not compatible with this system or executable
I tried the same in a different folder and it worked fine, no errors and env/bin has what I expect (activate, etc.).
I got the same problem and I found that it happens when you do not specify the python executable name properly. So for python 2x, for example:
virtualenv --system-site-packages -p python mysite
But for python 3.6 you need to specify the executable name like python3.6
virtualenv --system-site-packages -p python3.6 mysite
On on OSX 10.6.8 leopard, after having "upgraded" to Lion, then downgrading again (ouch - AVOID!), I went through the Wolf Paulus method a few months ago, completely ignorant of python. Deleted python 2.7 altogether and "replaced" it with 3.something. My FTP program stopped working (Fetch) and who knows what else relies on Python 2.7. So at that point I downloaded the latest version of 2.7 from python.org and it's installer got me up and running - until i tried to use virtualenv.
What seems to have worked for me this time was totally deleting Python 2.7 with this code:
sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7
removing all the links with this code:
sudo rm /usr/bin/pydoc
sudo rm /usr/bin/python
sudo rm /usr/bin/pythonw
sudo rm /usr/bin/python-config
I had tried to install python with homebrew, but apparently it will not work unless all of XTools is installed, which I have been avoiding, since the version of XTools compatible with 10.6 is ancient and 4GB and mostly all I need is GCC, the compiler, which you can get here.
So I just installed with the latest download from python.org.
Then had to reinstall easy_install, pip, virtualenv.
Definitely wondering when it will be time for a new laptop, but there's a lot to be said for buying fewer pieces of hardware (slave labor, unethical mining, etc).
The above solutions failed for me, but the following worked:
python3 -m venv --without-pip <ENVIRONMENT_NAME>
. <ENVIRONMENT_NAME>/bin/activate
curl https://bootstrap.pypa.io/get-pip.py | python
deactivate
It's hacky, but yes, the core problem really did just seem to be pip.
I did the following steps to get virtualenv working :
Update virtualenv as follows :
==> sudo pip install --upgrade virtualenv
Initialize python3 virtualenv :
==> virtualenv -p python3 venv
I had this same issue, and I can confirm that the problem was with an outdated virtualenv.py file.
It was not necessary to do a whole install --upgrade.
Replacing the virtualenv.py file with the most recent version sufficed.
I also had this problem, and I tried the following method which worked for me:
conda install virtualenv
virtualenv --system-site-packages /anaconda/envs/tensorflow (here envs keeps all the virtual environments made by user)
source /anaconda/envs/tensorflow/bin/activate
Hope it's helpful.
I had this same issue when trying to install py2.7 on a newer system. The root issue was that virtualenv was part of py3.7 and thus was not compatible:
$ virtualenv -p python2.7 env
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /Users/blah/env/bin/python
ERROR: The executable /Users/blah/env/bin/python is not functioning
ERROR: It thinks sys.prefix is u'/Library/Frameworks/Python.framework/Versions/2.7' (should be u'/Users/blah/env')
ERROR: virtualenv is not compatible with this system or executable
$ which virtualenv
/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenv
# install proper version of virtualenv
$ pip2.7 install virtualenv
$ /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv -p python2.7 env
$ . ./env/bin/activate
(env) $
Open terminal and type /Library/Frameworks/Python.framework/Versions/
then type ls /Library/Frameworks/Python.framework/Versions/2.7/bin/
if you are using Python2(or any other else).
Edit ~/.bash_profile and add the following line:
export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/2.7/bin/
cat ~/.bash_profile
In my case the content of ~/.bash_profile is as follows:
export PATH=$PATH:/Library/Frameworks/Python.framework/Versions/2.7/bin/
Now the virtualenv command should work.
If you continue to have trouble with virtualenv, you might try pythonbrew, instead. It's an alternate solution to the same problem. It works more like Ruby's rvm: It builds and creates an entire instance of Python, under $HOME/.pythonbrew, and then sets up some bash functions that allow you to switch easily between versions. Where virtualenv shadows the system version of Python, using symbolic links as part of its solution, pythonbrew builds entirely self-contained installations of Python.
I used virtualenv for years. It's a decent solution, but I've switched to pythonbrew lately. Having completely self-contained Python instances means that installing a new one takes awhile (since pythonbrew actually compiles Python from scratch), but the self-contained nature of each installation appeals to me. And disk is cheap.

Categories

Resources