Can't use Python virtualenv on OSX? - python

I'm having trouble with Python virtualenv on OSX. After creating a virtualenv and doing a pip freeze it says it's got a long list of things installed, which are actually the things I've got installed on my main pip installation (outside of the virtualenv):
kramer65$ virtualenv venv
New python executable in venv/bin/python
Installing setuptools, pip, wheel...done.
kramer65$ source venv/bin/activate
(venv)kramer65$ which pip
/Users/kramer65/repos/venv/bin/pip
(venv)kramer65$ pip freeze
apns==2.0.1
APScheduler==3.0.4
argh==0.26.1
Babel==1.3
beautifulsoup4==4.4.1
etc.
etc.
I've got virtualenv version 13.1.2, which I think is the latest.
So to be sure I tried calling pip with a direct link, but that gives the same result:
(venv)kramer65$ ./venv/bin/pip freeze
apns==2.0.1
APScheduler==3.0.4
etc.
etc.
or even by calling pip with the virtualenv python:
(venv)kramer65$ ./venv/bin/python venv/bin/pip freeze
apns==2.0.1
APScheduler==3.0.4
etc.
etc.
I also uninstalled and reinstalled virtualenv, but with the same result. Does anybody know why I can't seem to be able to call the Python of the installed virtualenv? All tips are welcome!
[EDIT]
As per the suggestion of #sushant below I tried using --no-site-packages:
kramer65$ virtualenv --no-site-packages venv
New python executable in venv/bin/python
Installing setuptools, pip, wheel...done.
kramer65$ source venv/bin/activate
(venv)kramer65$ pip freeze
apns==2.0.1
APScheduler==3.0.4
argh==0.26.1
Babel==1.3
beautifulsoup4==4.4.1
etc.
etc.
(venv)kramer65$ which pip
/Users/kramer65/repos/venv/bin/pip

The problem is that your virtualenv installation is include main site-packages too.
virtualenv will not include site-packages if you invoke it like so:
virtualenv --no-site-packages venv

Related

I used pip3 to install virtualenv but I can't create venv and also I can't uninstall virtualenv

I'm using Linux Mint 20.2 with two directories / and /home.
I used the following command to install virtualenv:
>>> sudo pip3 install virtualenv
It worked fine and it installed in the following path:
>>> virtualenv --version
virtualenv 20.0.17 from /usr/lib/python3/dist-packages/virtualenv/__init__.py
But when I tried to create an environment I got the following error:
>>> python3 -m venv article
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/username/article_tools/article/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
When I tried to uninstall it to install it using [b]sudo apt install python3.8-venv[/b], I got the following error:
>>> sudo pip3 uninstall virtualenv
Found existing installation: virtualenv 20.0.17
Not uninstalling virtualenv at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'virtualenv'. No files were found to uninstall.
How can I fix it? By fix, I mean installing virtualenv in a way that I don't get such errors.
The fundamental problem here seems to be that you are mixing up two different packages.
Python 3 comes with a built-in virtual environment module venv which is however not installed by default on Debian-based platforms. Like the error message says, apt-get install -y python3-venv will install this package, which you can then use with python3 -m venv.
virtualenv is a separate third-party package which you invoke with the command virtualenv. It's not a bad alternative, but if you are only just learning, I would suggest you simply ignore it for the time being.

Pip inside virtualenv install packages in the wrong place

So I tried to install flask package inside virtualenv.
$ virtualenv env
Using base prefix '/usr'
New python executable in /home/egor/Documents/python/projects/flask/env/bin/python3
Also creating executable in /home/egor/Documents/python/projects/flask/env/bin/python
Installing setuptools, pip, wheel...done.
$ . ./env/bin/activate
(env) $ sudo pip3 install flask
After this I tried to import flask package in python and got ImportError exception. (env) $ pip3 freeze gave me nothing.
Then I've discovered package in /usr/local/lib/python3.5/dist-packages directory.
So basically pip3 inside vurtualenv install packages in the wrong place.
How can I solve this problem? Is it because of Using base prefix '/usr' line?
I believe that pip doesn't need root privilege (especially in a virtual env). Try pip3 install flask without sudo.

osx install packages inside virtualenv

I tried to start virtualenv WITHOUT sudo but unfortunately it cannot find (Permission denied) /lib/python2.7/site-packages/easy_install.py. So I did:
sudo virtualenv name_env
The problem is that now pip is the global version (not inside pip):
which pip:
/usr/local/bin/pip
So I cannot install any package inside the environment.
If I start virtualenv without sudo:
virtualenv name_env
OSError: Command /Users/andrea/package_lambda/bin/python2.7 -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip wheel failed with error code 2
Any suggestion?
Don't use sudo just because you can!
I suggest you install another Python environment using brew, and then install pip, and subsequently virtualenv. This way, you'll substantially correct the underlying problem.
I would follow this method:
brew install pyenv
pyenv install 2.7.11
Or check the available versions through:
pyenv versions
This way, you can install different versions and switch between them as you wish, for instance:
pyenv global 2.7.11
And then you can install pip like so:
python -m easy_intall pip
and then install virtualenv like so:
python -m pip install virtualenv
As pouria mentioned, I believe it's a good idea to make sure you installed virtualenv using pip in the first place. I also agree that on OSX, using sudo should be rare.
As mentioned on a previous answer, you should also check that the files in the bin of your virtual env are correct.
I found the solution myself. I was using iterm instead of terminal (standard mac OS X). Using terminal I did:
sudo pip uninstall virtualenv
sudo pip install virtualenv
sudo cp /usr/local/bin/virtualenv /bin/virtualenv
Then I can create start a virtualenv:
virtualenv name_env
source name_env/bin/activate
To install python package on it I use:
sudo pip install --target=name_env/lib/python2.7/site-packages/ package name
I have the following file ~/.pydistutils.cfg with the contents
[install]
prefix=
temporarily removing this file fixed the issue for me (i had this file in place to address a different issue)

how to install setuptools in mac

I'm trying to install via pip some libraries but I'm having some problems.
When I try to install some of them I require for my project I get this message:
$ sudo pip install dj-database-url==0.2.0
Downloading/unpacking dj-database-url==0.2.0
Downloading dj-database-url-0.2.0.tar.gz
Cleaning up...
setuptools must be installed to install from a source distribution
It also happens when trying to install distribute==0.6.24
Any ideas?
Download ez_setup.py module from https://pypi.python.org/pypi/setuptools
Open a Terminal.
cd to the directory where you put the ez_setup.py.
Type python ez_setup.py and run it.
You should have it then.
After a pip install command I was getting the same error as you ("setuptools must be installed to install from a source distribution"). Since I couldn't find a solution, it was a lot faster to reinstall the virtual environment where python was running from.
If you're using virtualenvwrapper this is very easy. First you remove your problematic virtual environment (let's say it is called "venv") with:
rmvirtualenv venv
Then you setup a new one with the same name:
mkvirtualenv venv
And finally you install all your packages, including the one you had problems with:
pip install dj-database-url

Error With Installing Virtualenv With Pip and Homebrew

I am trying to get a nice clean Python environment setup on OSX 10.9. I've installed Python with Homebrew and set my PATH variables so...
> which python
/usr/local/bin/python
and
> which pip
/usr/local/bin/pip
so when I look at my /usr/local/bin :
pip -> ../Cellar/python/2.7.6/bin/pip
python -> ../Cellar/python/2.7.6/bin/python
then when I run:
> pip install virtualenv
I get permission errors on /usr/local/bin/virtualenv:
...
running install_scripts
Installing virtualenv script to /usr/local/bin
error: /usr/local/bin/virtualenv: Permission denied
I thought that by using Homebrew I could use pip and avoid using sudo to install virtualenv. Am I doing something wrong here?
Ok! I managed to fix this myself.
I deleted all the virtualenv related things from /usr/local/bin (they had been installed under root for some reason and this was causing my permission issues.).
Then I did a pip uninstall virtualenv to get rid of other instances of virtualenv, as there was still one in /usr/local/lib/python2.7/site-packages/
Then a simple pip install virtualenv and things work fine now!
Most likely HomeBrew does some magic so that running brew install allows writing to /usr/local/bin, but this privilege is not available to normal commands. This is a guess, I didn't investigate this further.
Install virtualenv with brew:
brew install pyenv-virtualenv
This command:
pip install virtualenv
runs pip from your first directory from $PATH environment variable, which is usually system wide, thus when you run in this way - you are trying to install it globall.
You should install you your environment in your $HOME directory:
virtualenv $HOME/myvirpython
and later:
$HOME/myvirpython/bin pip install something
Additionally you should read this:
https://docs.brew.sh/Homebrew-and-Python

Categories

Resources