Use homebrew to install applications to virtual enviornment - python

Is there anyway I can use homewbrew to install packages (like numpy or matplotlib) into isolated virtual environments created using virtualenv, without having the packaged installed system wide.

Use pip inside of the virtualenv and it will isolate the packages to just that virtualenv. Each virtualenv has a local version of pip and will install the packages locally.

You can install numpy with pip.
Your problem will probably solved by issuing the following commands:
$ export CFLAGS=-Qunused-arguments
$ export CPPFLAGS=-Qunused-arguments
$ pip install numpy
You can test with
$ python -c 'import numpy'
There are several packages which have this problem at the moment. PIL is another example.

Related

Install pandas in debian10 on a different python version

I have a device with python3.7 preinstalled, in which i have installed also python3.9. I managed to change the version I am using of python3 and now the command "python3" followed by the .py file runs with python3.9.
The problem is I tried installing pandas with pip3 but it does not work (it didn't work even in the preinstalled python3.7), so I found that in debian you can install package, for example in this case pandas, using "sudo apt-get install python3-pandas" but this command keeps installing pandas in python3.7 and not in python3.9 even if now "python3" refers to python3.9.
Has anyone ever encountered this problem and has a solution?
python3.9 -m pip install pandas
Venv
You could use a virtual environment (venv) for installing dependencies.
This venv could be project specific or global.
Run python3 -m venv .venv in your project folder to create a .venv folder, which holds the venv configuration.
Run source .venv/bin/activate to activate the venv. This will link pip3 from your python 3.9 version to the pip command.
Now you can do pip install pandas to install the pandas dependency into the venv.
Conda
Another solution would be to use Anaconda or Miniconda
https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html
conda create -n name_of_my_env python
This will create a minimal environment with only Python installed in
it.
To put your self inside this environment run:
source activate name_of_my_env
On Windows the command is:
activate name_of_my_env
The final step required is to install pandas. This can be done with
the following command:
conda install pandas

Problems of VirtualEnv and pip3

I used VirtualEnv to create a python2 environment without system site packages like this:
virtualenv -p /usr/bin/python2.7 --no-site-packages ENV2.7
And I want to install packages in this environment.
However, I found that my python code is still trying to look for packages out of this environment.
For example, after activate this env, I used:
pip install matplotlib
And in my demo.py, there is
import matplotlib
But this raised an error, and can not find this package
However, when I use python in the terminal and enter the interactive python, import matplotlib dose not raise an error.
Then I started another terminal and tried to install this package out of the environment by pip3:
pip3 install matplotlib
It turned out that my demo.py just work well.
Any idea? Many Thanks!
It sounds like your virtualenv pip version may be using pip3 instead of pip2:
Make sure you are using the correct python version in your project that you mean to, and using the same version of pip in your virtualenv. (Note that you use pip above once, then you used pip3 outside your virtualenv.)
Check your pip version from inside the virtualenv:
workon (your env name)
which pip
pip -V
Output should look something like:
$ which pip
/home/yourname/.virtualenvs/testenv/bin/pip
$ pip -V
pip 9.0.1 from /home/yourname/.virtualenvs/testenv/local/lib/python2.7/site-packages (python 2.7)
It should tell you you're using pip inside your virtualenv, and the correct python version.
If that looks correct, install your packages.
pip install (whatever)
Check they are installed with pip freeze.
Run your project. :)

install pip3 for conda

Python2.6 was installed by default in my old centos server. Now I want to create a Python3 environment to install python3 specific module by conda
conda create -n py3 python=3.5.3
source activate py3
After activate the py3, I try to install hovercraft by pip3 install hovercraft, the shell tells "command not found: pip3". At first I thought pip3 was installed with Python3, but the result was not the case.
So I think I can install it manually. The package gzip file was downloaded from python package index, and install by conda install --file hovercraft-2.3.tar.gz. But it doesn't work.
Now I have two problems:
how to install pip3 for virtual-env create by conda?
Is it possible to install python package index downloaded package locally in conda?
pip3 and pip would make a difference only when you are not using any environment managers like virualenv (or) conda. Now as you are creating a conda environment which has python==3.x, pip would be equivalent to pip3.

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)

Can't import PIL after installing Pillow

I have python 3.x, and was told to install Pillow for image manipulation. After installing it with pip however, i'm unable to import PIL from the python interpreter. It just says ImportError: No module named 'PIL'. Running pip list in the command line shows that Pillow is indeed installed.
It ended up installing correctly after using easy_install instead of pip.
You might have used pip for a different python executable. To make sure that the correct pip command is used, run:
$ python3 -m pip install pillow
I don't really have the time or the means to create an entire virtual machine just for the purposes of testing this one problem.
virtualenv is not a virtual machine. All you need to create a virtualenv from scratch:
$ python3 -m pip install --user virtualenv # install package
$ python3 -m virtualenv venv # create virtualenv named 'venv'
Then to activate the created virtualenv on POSIX system:
$ source venv/bin/activate # activate on POSIX
Or on Windows:
C:\> venv\Scripts\activate
Now, python, pip commands refers to the virtualenv. To deactivate virtualenv, run:
$ deactivate

Categories

Resources