virtualenv doesn't work in mac with anaconda - python

I have virtualenv installed and also 2 versions of python installed. One is through homebrew another is in anaconda. But virtualenv doesn't work with either of them. I found some posts saying this is due to messy python version. But even I point to different python, it still doesn't work. Is there any solution for this ? Thanks
jzhangMBPr:~ jzhang$ virtualenv -p /Users/jzhang/anaconda/bin/python a
Already using interpreter /Users/jzhang/anaconda/bin/python
Using base prefix '/Users/jzhang/anaconda'
New python executable in /Users/jzhang/a/bin/python
ERROR: The executable /Users/jzhang/a/bin/python is not functioning
ERROR: It thinks sys.prefix is '/Users/jzhang' (should be '/Users/jzhang/a')
ERROR: virtualenv is not compatible with this system or executable
jzhangMBPr:~ jzhang$ virtualenv a
Using base prefix '/Users/jzhang/anaconda'
New python executable in /Users/jzhang/a/bin/python
ERROR: The executable /Users/jzhang/a/bin/python is not functioning
ERROR: It thinks sys.prefix is '/Users/jzhang' (should be '/Users/jzhang/a')
ERROR: virtualenv is not compatible with this system or executable

You probably pointing virtualenv to the wrong Python installation. I thought its regardless of which python interpreter you have. Virtualenv comes with a -p flag which let you specifically which interpreter in use.
Use;
virtualenv -p python test
Instead;
virtualenv test
Also you can use virtualenv -h, which appears an help documentation for usage of other specific virtualenv flags.

Please check your conda init. For me running :
conda init bash
solved the problem.

Related

Mac OSX Catalina Broke All Python Venv's

Just recently upgraded to Mac OSX Catalina, and it seems to have broken all my Python venv's.
I have a venv that I previously created before upgrading to Catalina.
I can activate the venv without issue: source venv/bin/activate
When I type python or python3, it actually runs the system Python interpreters, instead of the expected Python interpreter in my venv.
With my venv activated...
>>> which python
/usr/bin/python
>>> which python3
/usr/local/bin/python3
I expect that to point to /Users/<username>/<path-to-venv>/venv/bin/python3.
If I try to run that Python interpreter directly:
>>> /Users/<username>/<path-to-venv>/venv/bin/python3
bash: /Users/<username>/<path-to-venv>/venv/bin/python3: No such file or directory
If I navigate to this venv directory in Finder, I can see that the python3 file exists, although it's actually an alias. If I double-click on it, I get an error message that pops up that reads: The operation can't be completed because the original item for "Python3" can't be found.
So I think the way these venv's work is that they are based on references to existing Python interpreter installations. Evidently running a venv invokes these references, and if the original installation is broken it fails. I know that Catalina jacked with the default Python settings and went so far as to change installation directories.
Has anyone else encountered this? Can anyone offer tips to fix this? This seems to affect all my venv virtual environments across my system.
You can try to create a new venv in the same folder or to update your existing venv. With all the following I assume you've created your virtual environments with the bundled venv module
For the creation it's the command you've probably already used
python3 -m venv <path_to_dir>
In order to update, the --upgrade parameter is what you're looking for.
You need to run it with the targeted Python version, for example in this case:
python3 -m venv --upgrade <path_to_dir>
for more details, see my answer here

Python - Installing packages in Virtual Env

I am trying virtual env for the first time in python. I have Python 3.6 and Python 2.7 installed in my machine. I have installed all the necessary packages in Python 3.6. However, the code that I have seems to be compatible with Python 2.7. Hence I opted for Virtaul Env. But I could not access the packages that i need. I have went through the questions in stack overflow but nothing helped.
I have tried following steps:
Created new project in pycharm
used the interpreter as Python27
in command prompt, I went to specific virtual env folder and tried
pip install package_name
showing the error as below
Fatal Python error: Py_Initialize: unable to load the file system codec
File "D:\Users\username\venv_name\lib\encodings\__init__.py", line 123
raise CodecRegistryError,\
^
SyntaxError: invalid syntax
Current thread 0x0000d85c (most recent call first):
I need either to install packages in the venv or transfer already installed packages into venv.
Thanks
In order for you to install packages to your virtual environment you need to add it first in PyCharm and use it as your interpreter.
You can add your venv by:
By going to Settings(Ctrl+Alt+s in windows)
Click the ⚙ icon next to project interpreter then choose add.
Browser for your virtual environment.
After that you can now install packages to your virtual env by clicking the ➕ icon.
Also you cannot just install packages in your virtual environment by going to its directory, you need to activate it first by:
Windows: c:\path\to\your\venv\Scripts\activate.bat
Linux/Mac: source path/to/your/venv/bin/activate
I don't know how to solve your problem, since I don't use pycharm myself, but I can tell you what's causing it.
It seems that you are trying to run Python 2 code under Python 3. The raise expression that fails for you is valid under Python 2, but not under Python 3. Therefore the SyntaxError. So, you are probably using the source files from the virtualenv (see that ...\venv_name\... path?), but executing them in Python 3 interpreter.
From another SO post, the issue is PYTHONPATH. Clear it, and you should be able to use 2 and 3.

virtualenv -p dropping last part of sys.prefix filepath when creating a new virtual environment

When I try to create a new virtual environment using The Enthought Canopy distribution of python I keep experiencing the below issue
localhost:workspace brendan$ virtualenv -p /Users/brendan/Library/Enthought/Canopy_64bit/User/bin/python test
Running virtualenv with interpreter /Users/brendan/Library/Enthought/Canopy_64bit/User/bin/python
Using base prefix '/Applications/Canopy.app/appdata/canopy-1.4.1.1975.macosx-x86_64/Canopy.app/Contents'
New python executable in test/bin/python
dyld: Library not loaded: #rpath/Python
Referenced from: /Users/brendan/Dropbox/workspace/test/bin/python
Reason: image not found
ERROR: The executable test/bin/python is not functioning
ERROR: It thinks sys.prefix is u'/Users/brendan/Dropbox/workspace' (should be u'/Users/brendan/Dropbox/workspace/test')
ERROR: virtualenv is not compatible with this system or executable
localhost:workspace brendan$
I am not sure why it is always dropping the last part of sys.prefix or how I should go about fixing this.
This is related to a previous question I posted in which this was the suggested work around.
Any help is much appreciated
Thanks
Canopy is built on a backport of Python 3's venv. Therefore virtualenv cannot be used in Canopy Python. Most virtualenv users should be able to use venv in its place, without difficulty. However we recognize that this does pose problems for some users. Therefore the next major release of Canopy (no ETA -- not in the next few months) will support either option.

How to use virtualenv in 32-bit Python on a 64-bit machine?

I've been plugging away at this for a few hours now and am having a heck of a time.
The 3rd error says to copy the DLL, meanwhile it is already there by default.
C:\Windows\system32>virtualenv django-josh
New python executable in django-josh\Scripts\python.exe
ERROR: The executable django-josh\Scripts\python.exe is not functioning
ERROR: It thinks sys.prefix is u'c:\\windows\\syswow64\\django-josh' (should be u'c:\\windows\\system32\\django-josh')
ERROR: virtualenv is not compatible with this system or executable
Note: some Windows users have reported this error when they installed Python for "Only this user" or have multiple versions of Python installed. Copying the appropriate PythonXX.dll to the virtualenv Scripts/ directory may fix this problem.
I've been reading this and want to note, I had Python 3 installed a while back, but then installed Python 2.7 and removed Python 3 after. I'm wondering if there's still a trace of Python 3 on my computer somewhere, even though I can't find anything.
Update: I was able to get it working with this command (using an option), but I'm not sure if that's correct.
C:\Windows\system32>virtualenv --distribute C:\Python27\envs\django
New python executable in C:\Python27\envs\django\Scripts\python.exe
Installing setuptools, pip...done.
Thanks so much!
--distribute uses Distribute, a fork of Setuptools that is no longer maintained. It has since been merged back into Setuptools. In general, I'd recommend against using this option. You can use the -p option with the path to an executable to specify which Python interpreter to use. That way, you can be sure you're not using Python 3. It looks like you're creating your environment under C:\Python27, but I'm not convinced that your virtualenv will know to use 2.7 just by having it in that location.

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