Installing Scipy in Virtualenv - python

I've tried many ways to install scipy but without successes. I use virtualenv on Linux Mate.
After the workon command I tried
pip install scipy
or
easy_install scipy
end every time is a "failed with exit status 1"
I tried also
apt-get install python-scipy
and I've no errors but if I run python
python
import scipy
I'll have "ImportError: No module named scipy"
What is wrong?

I'd suggest using Miniconda to install scipy.
wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh
bash Miniconda-latest-Linux-x86_64.sh -b
Then, create a Conda environment with scipy installed:
conda create -n scipy scipy
Now you'll have access to the activate and deactivate scripts, used to open and close Conda environments.
$ source activate scipy
discarding /Users/username/miniconda/bin from PATH
prepending /Users/username/miniconda/envs/scipy/bin to PATH
(scipy)$ python
Python 2.7.11 |Continuum Analytics, Inc.| (default, Dec 6 2015, 18:57:58)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import scipy
>>> scipy.version.version
'0.16.0'
[Edit] You indicated you want to use the system installed scipy package, that you have successfully installed via apt, in a virtualenv. You can instruct virtualenv to create a new environment that includes all the system Python packages like this:
$ virtualenv --system-site-packages scipy_env
New python executable in scipy_env/bin/python
Installing setuptools, pip...done.
$ source scipy_env/bin/activate
(scipy_env)$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy

Related

How do I make sure conda's environment-specific paths are at the top of sys.path?

I have a conda environment, myenv. I realized that the Python interpreter in this environment tries to import packages from /Users/me/.local/lib/python3.8/site-packages before /usr/local/anaconda3/envs/myenv/lib/python3.8/site-packages. I would have expected all the environment-specific paths to be appended to the beginning of sys.path. Is this expected behavior? In this case, I want to import conda-installed versions of numpy, scipy and numexpr since they use the Intel MKL backend.
(myenv) me$ which python
/usr/local/anaconda3/envs/myenv/bin/python
(myenv) me$ python
Python 3.8.5 (default, Sep 4 2020, 02:22:02)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__file__
>>> '/Users/me/.local/lib/python3.8/site-packages/numpy/__init__.py'
>>> import sys
>>> # /Users/me/dir* refer to directories that contain local modules I've pip installed
>>> print("\n".join(sys.path))
/usr/local/anaconda3/envs/myenv/lib/python38.zip
/usr/local/anaconda3/envs/myenv/lib/python3.8
/usr/local/anaconda3/envs/myenv/lib/python3.8/lib-dynload
/Users/me/.local/lib/python3.8/site-packages
/Users/me/dir1
/Users/me/dir2
/Users/me/dir3
/usr/local/anaconda3/envs/myenv/lib/python3.8/site-packages
Other things that might be relevant:
I use conda and pyenv side-by-side so have conda's auto_activate_base setting set to false (as suggested here).
The Python interpreter declares that it's version 3.8.5, but conda info lists python version as 3.8.3.final.0.
I'm using conda version 4.9.2 on a MacOS Version 11.2.2.
Thanks to #merv's comment, I realized that the issue was that pip's user-site is prioritized above the conda environment's package directory. I resolved this as #merv suggested: by uninstalling all the packages in pip's user-site.
me$ pyenv shell 3.8.1
me$ pip freeze --user | grep -Eo "[[:alnum:]\-]+==[[:digit:]]{1,2}\.[[:digit:]]{1,2}(\.[[:digit:]]{1,2})?" | xargs pip uninstall -y
me$ conda activate myenv
(myenv) me$ python
Python 3.8.5 (default, Sep 4 2020, 02:22:02)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__file__
/usr/local/anaconda3/envs/myenv/lib/python3.8/site-packages/numpy/__init__.py

Build Python as UCS-4 via pyenv

I run into this issue ImportError numpy/core/multiarray.so: undefined symbol: PyUnicodeUCS2_AsASCIIString installing Python in a pyenv-virtualenv environment.
In my case, it happens with the matplotlib package instead of numpy (as in the above question), but it's basically the same issue.
The answer given in that question is a simple:
Rebuild NumPy against a Python built as UCS-4.
I don't know how to do this. In this other question it is said that one has to use:
./configure --enable-unicode=ucs4
but I don't know how to use that command along with pyenv.
This issue is also mentioned in pyenv's repo issue list, and a solution given in a comment. Sadly (for me) I can not understand how to apply the fix explained in said comment.
So my question basically is: how do I build Python as UCS-4 via pyenv?
Installing python with pyenv with ucs2:
$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2
$ pyenv install -v 2.7.11
...
$ pyenv local 2.7.11
$ pyenv versions
system
* 2.7.11 (set by /home/nwani/.python-version)
$ /home/nwani/.pyenv/shims/python
Python 2.7.11 (default, Aug 13 2016, 13:42:13)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_config_vars()['CONFIG_ARGS']
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs2' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '"
Installing python with pyenv with ucs4:
$ pyenv uninstall 2.7.11
pyenv: remove /home/nwani/.pyenv/versions/2.7.11? y
$ export PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs4
$ pyenv install -v 2.7.11
...
$ pyenv local 2.7.11
$ pyenv versions
system
* 2.7.11 (set by /home/nwani/.python-version)
$ /home/nwani/.pyenv/shims/python
Python 2.7.11 (default, Aug 13 2016, 13:49:09)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_config_vars()['CONFIG_ARGS']
"'--prefix=/home/nwani/.pyenv/versions/2.7.11' '--enable-unicode=ucs4' '--libdir=/home/nwani/.pyenv/versions/2.7.11/lib' 'LDFLAGS=-L/home/nwani/.pyenv/versions/2.7.11/lib ' 'CPPFLAGS=-I/home/nwani/.pyenv/versions/2.7.11/include '"

Wrong Django version returned in virtualenv

I'm on a Mac running OS X !0.10 Yosemite. Default versions of Python & Django are 2.7 & 1.5. I'm want to set up a virtualenv that has Django 1.8 so I'm doing the following:
$ virtualenv --no-site-packages django18env
New python executable in django18env/bin/python2.7
Also creating executable in django18env/bin/python
Installing setuptools, pip...done.
$ source django18env/bin/activate
(django18env)$
Then I'm installing Django 1.8
(django18env)$ sudo pip install django==1.8
Password:
Downloading/unpacking django==1.8
Downloading Django-1.8-py2.py3-none-any.whl (6.2MB): 6.2MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
(django18env)$
Once that has run I have Django installed under django18env/lib/python2.7/site-packages/django
If I look at the __init__.py file in that directory it shows:
from django.utils.version import get_version
VERSION = (1, 8, 0, 'final', 0)
So it certainly looks like the right version is installed in the virtualenv directory. However, if I use django-admin --version I get:
(django18env)$ django-admin version
1.5.4
I've also tried starting python in the virtual env and getting the django version that way:
(django18env)$ python
Python 2.7.8 (default, Jul 29 2014, 21:50:48)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.5.4'
>>>
Any ideas on why it still seems to be pointing to 1.5 when 1.8 is installed in the vertualenv?
I've read various other threads on here but can't get the version to point to 1.8
Any help much appreciated
Thanks
don't use sudo on virtualenv. the point of vitualenv is, to install software not system wide, but verily for that enviroment. but no matter inside a virtualenv or outside it, if you use sudo, it will install software to your system globally.
ziya#ziya:~/Desktop/coursera/python/lorem$ virtualenv ipsum
New python executable in ipsum/bin/python2.6
Also creating executable in ipsum/bin/python
Installing setuptools, pip...done.
#created a virtualenv
ziya#ziya:~/Desktop/coursera/python/lorem$ cd ipsum/
ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ . bin/activate
# will now install package with sudo
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ sudo pip install sudokulib # i don't know what it is, just installing.
[sudo] password for ziya:
.....
Collecting sudokulib
/usr/local/lib/python2.7/dist-packages # attention to this path!
...
Successfully installed sudokulib-0.6a0
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sudokulib # import the newly installed module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sudokulib
>>> exit()
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ deactivate
#deactivating virtualenv and starting default python
ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sudokulib
>>> sudokulib.__version__
'0.6a' #here it is!
>>> exit()
ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ . bin/activate
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ pip install sudokulib #now installing the same module without sudo
Collecting sudokulib
Downloading sudokulib-0.6a.tar.gz
/home/ziya/Desktop/coursera/python/lorem/ipsum/lib/python2.6/site-packages
....
Successfully installed sudokulib-0.6a0
(ipsum)ziya#ziya:~/Desktop/coursera/python/lorem/ipsum$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sudokulib
>>> sudokulib.__version__
'0.6a' #seems ok now :)
>>>
I found the issue. As I say above, Django 1.8 was being installed in the virtualenv OK but Python wasn't using it. In the vitualenv I started Python and then:
>>>import django
>>>django.__file__
This showed that Django had been imported from:
/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
And when I looked in my .bash_profile there was a line:
export PYTHONPATH=$PYTHONPATH:/usr/local/Cellar/python/2.7.8/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Not sure how/when this got in there but I commented it out, restarted the shell and I now get Django1.8 in my virtualenv and the older (default) version 1.5 outside the virtualenv.
Thanks for the help and suggestions

How to remove a module using Anaconda in Python

I have a package called 'Prody' which is installed under anaconda directories:
$ python
Python 2.7.8 |Anaconda 2.0.1 (x86_64)| (default, Jul 2 2014, 15:36:00)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import prody
>>> prody.__file__
'//anaconda/lib/python2.7/site-packages/ProDy-1.5.1-py2.7-macosx-10.5-x86_64.egg/prody/__init__.pyc'
I'd like to remove that file with this command but failed:
$ conda remove prody
Error: no packages found to remove from environment: //anaconda
What's the right way to do it?
You can remove packages installed with pip using pip uninstall.
Did you install Prody via pip? Then I don't think that conda remove can remove it. AFAIK, conda manages only conda packages.

Python path: why can't pip see latest version of numpy

Working inside a virtualenv, I'm trying to install scikit-image, but it fails, telling me that I need a newer (or any) version of numpy.
$ sudo pip install -U scikit-image
...
ImportError: You need `numpy` version 1.6 or later.
----------------------------------------
Command python setup.py egg_info failed with error code 1 in /Users/aps/Dropbox/experiments/build/scikit-image
I don't understand this, because from Python, numpy 1.7 is already on my path.
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__version__
'1.7.1'
What is going on - why can't the system installing scikit-image see the newest version of numpy? And how can I fix this?
The path to numpy already seems to be in my ~/.bash_profile:
>>> numpy.__path__
['/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy']
>>> exit()
$ vi ~/.bash_profile
...
export PYTHONPATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH
UPDATE:
numpy version 1.7 is also in the su path:
$ sudo python
Password:
Python 2.7.5 (default, Aug 1 2013, 00:59:40)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__version__
'1.7.1'
You need to make sure that all those packages are available within the virtualenv itself. Did you try running pip freeze after sourcing bin/activate? Using the virtualenv package, the only library that comes "preinstalled" by default is distribute, and all other dependencies must be manually added. With venv (built into py3k) all of your system libraries should be available directly. Which are you using?

Categories

Resources