Поиск по блогу

вторник, 22 апреля 2014 г.

К статье "Python Packages and Environments with conda"

Второй раз сталкиваюсь с "A Virtual Environment", что это такое и зачем оно ? How to use Python virtualenv?
"...put simply, is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects"
Оказывается, что для сборки Anaconda нужно использовать специальные команды утвновщика Conda
Еще лучше идея "виртуального окружения" поясняется в статье "Python Packages and Environments with conda". Ребята из "continuum.io" создали Anaconda именно для того, чтобы снять проблемы соглавования версий и обмена данными между членами научных сообществ. Грузить все в облако Wakari иначе просто нельзя.
Другой важный аспект использования "Virtual Environment" заключается в том, что задачи,которые приходится решать настолько разнообразны, что единое рабочее простанство нельзя раздувать до бесконечности, проще использовать несколько компактных. Anaconda позволяет производить такие настройки.

Первоисточники и ссылки

In [3]:
from IPython.display import HTML, Image
import pattern.web
# We have to determine 'tag1+tag2+tag3' for delicious
# And number of items for parsing from RSS
number=5
tags='anaconda'
url = 'http://delicious.com/v2/rss/altersego2007/' + tags
results = pattern.web.Newsfeed().search(url, count=number)
str=''
for result in results:
    str = str + '<br/><a href='+ result.url + ' target="_blank">' + 'ref ' + result.title + '</a>  ' + result.text
HTML(str)
Out[3]:

ref Introduction — Conda documentation The conda command is the primary interface for managing installations of various packages. It can query and search the package index and current installation, create new environments, and install and update packages into existing conda environments.
ref Spyder - Documentation — Spyder 2.2 documentation Spyder is the Scientific PYthon Development EnviRonment: a powerful interactive development environment for the Python language with advanced editing, interactive testing, debugging and introspection features and a numerical computing environment thanks to the support of IPython (enhanced interactive Python interpreter) and popular Python libraries such as NumPy (linear algebra), SciPy (signal and image processing) or matplotlib (interactive 2D/3D plotting). Spyder may also be used as a library providing powerful console-related widgets for your PyQt-based applications – for example, it may be used to integrate a debugging console directly in the layout of your graphical user interface.
ref Anaconda — Continuum documentation Anaconda is a free collection of powerful packages for Python that enables large-scale data management, analysis, and visualization for Business Intelligence, Scientific Analysis, Engineering, Machine Learning, and more.
ref Windows Installation — Canopy 1.1.1-final documentation Это аналог анаконды (?) Ссылка на инструкцию по установке

Далее выдержки из "Python Packages and Environments with conda"

Having been involved in the python world for so long, we are all aware of pip, easy_install, and virtualenv, but these tools did not meet all of our specific requirements.
The main problem is that they are focused around Python, neglecting non-Python library dependencies, such as HDF5, MKL, LLVM, etc., which do not have a setup.py in their source code and also do not install files into Python’s site-packages directory.
Under the hood, we have created a concept of environments which are conceptually similar to virtualenvs, but which use filesystem-level hard links to create entirely self-contained Python runtime layouts.
By using the ‘conda’ command line tool, users can easily switch between environments, create environments, and install different versions of libraries and modules into them.

Creating and Using Environments

At the core, Anaconda environments are just like directories that contain particular versions of packages. These can be located anywhere, but if they are within the Anaconda installation directory, conda will know about them. Let’s take a look:
In [6]:
!conda info -h
usage: conda-script.py info [-h] [-a] [-e] [-l] [-s] [args [args ...]]

Display information about current conda install.

positional arguments:
  args           display information about packages or files

optional arguments:
  -h, --help     show this help message and exit
  -a, --all      show all information, (environments, license, and system
                 information
  -e, --envs     list all known conda environments
  -l, --license  display information about local conda licenses list
  -s, --system   list environment variables

In [7]:
!conda info --envs
# conda environments:
#
root                  *  C:\Users\kiss\Anaconda


In [8]:
!conda info -s
sys.version: 2.7.5 |Anaconda 1.9.2 (64-bit)| (default...
sys.prefix: C:\Users\kiss\Anaconda
sys.executable: C:\Users\kiss\Anaconda\python.exe
conda location: C:\Users\kiss\Anaconda\lib\site-packages\conda
conda-build: None

CIO_TEST: <not set>
CONDA_DEFAULT_ENV: <not set>
CONDA_ENVS_PATH: <not set>
PATH: C:\Users\kiss\Anaconda\lib\site-packages\numpy\core;C:\Program Files\ImageMagick-6.8.8-Q8;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Java\jdk1.7.0_21\bin;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\HashiCorp\Vagrant\bin;C:\Users\kiss\Anaconda\Scripts;C:\Program Files\cURL\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin\;C:\Program Files\HDF_Group\HDF5\1.8.12\bin;C:\Users\kiss\Anaconda;C:\Users\kiss\AppData\Local\Pandoc\
PYTHONHOME: <not set>
PYTHONPATH: <not set>


A fresh install, there is just the default environment. Now we’d like to create some new environments.
Maybe we have some existing libraries that perform some interesting analysis, and we’d like to test and compare our library with NumPy 1.6 and also the upcoming NumPy 1.7 release. Let’s see what versions of NumPy are available on our known package channels:
In [10]:
!conda search -h
usage: conda-script.py search [-h] [-n NAME | -p PATH] [--canonical] [--known]
                              [--use-index-cache] [-o] [-v]
                              [--platform {win-32,win-64,osx-64,linux-32,linux-64}]
                              [-c CHANNEL] [--override-channels]
                              [regex]

Search for packages and display their information. The input is a
regular expression.  To perform a search with a search string that starts with
a -, separate the search from the options with --, like 'conda search -- -h'.

positional arguments:
  regex                 package specification or regular expression to search
                        for (default: display all packages)

optional arguments:
  -h, --help            show this help message and exit
  -n NAME, --name NAME  name of environment (in C:\Users\kiss\Anaconda\envs)
  -p PATH, --prefix PATH
                        full path to environment prefix (default:
                        C:\Users\kiss\Anaconda)
  --canonical           output canonical names of packages only
  --known               only use index metadata from the known remote
                        channels, and not the local package cache (which are
                        from unknown channels)
  --use-index-cache     use cache of channel index files
  -o, --outdated        only display installed but outdated packages
  -v, --verbose         Show available packages as blocks of data
  --platform {win-32,win-64,osx-64,linux-32,linux-64}
                        Search the given platform. Should be formatted like
                        'osx-64', 'linux-32', 'win-64', and so on. The default
                        is to search the current platform.
  -c CHANNEL, --channel CHANNEL
                        additional channel to search for packages. These are
                        URLs searched in the order they are given (including
                        file:// for local directories). Then, the defaults or
                        channels from .condarc are searched (unless
                        --override-channels is given). You can use 'defaults'
                        to get the default packages for conda, and 'system' to
                        get the system packages, which also takes .condarc
                        into account. You can also use any name and the
                        .condarc channel_alias value will be prepended. The
                        default channel_alias is http://conda.binstar.org/
  --override-channels   Do not search default or .condarc channels. Requires
                        --channel.

examples:
    conda search -p ~/anaconda/envs/myenv/ scipy

In [11]:
!conda search numpy
Fetching package metadata: ..
numpy                        1.6.2                    py27_0  defaults        
                             1.6.2                    py26_0  defaults        
                             1.6.2                   py27_p4  defaults        [mkl]
                          .  1.6.2                    py27_4  defaults        
                             1.6.2                   py26_p4  defaults        [mkl]
                             1.6.2                    py26_4  defaults        
                             1.6.2                   py27_p5  defaults        [mkl]
                             1.6.2                   py26_p5  defaults        [mkl]
                             1.7.0                    py33_0  defaults        
                             1.7.0                    py27_0  defaults        
                             1.7.0                    py26_0  defaults        
                             1.7.1                   py33_p0  defaults        [mkl]
                             1.7.1                    py33_0  defaults        
                             1.7.1                   py27_p0  defaults        [mkl]
                             1.7.1                    py27_0  defaults        
                             1.7.1                   py26_p0  defaults        [mkl]
                             1.7.1                    py26_0  defaults        
                             1.7.1                   py33_p1  defaults        [mkl]
                             1.7.1                    py33_1  defaults        
                             1.7.1                   py27_p1  defaults        [mkl]
                             1.7.1                    py27_1  defaults        
                             1.7.1                   py26_p1  defaults        [mkl]
                             1.7.1                    py26_1  defaults        
                             1.7.1                   py33_p2  defaults        [mkl]
                             1.7.1                    py33_2  defaults        
                             1.7.1                   py27_p2  defaults        [mkl]
                             1.7.1                    py27_2  defaults        
                             1.7.1                   py26_p2  defaults        [mkl]
                             1.7.1                    py26_2  defaults        
                             1.7.1                   py33_p3  defaults        [mkl]
                             1.7.1                    py33_3  defaults        
                             1.7.1                   py27_p3  defaults        [mkl]
                          .  1.7.1                    py27_3  defaults        
                             1.7.1                   py26_p3  defaults        [mkl]
                             1.7.1                    py26_3  defaults        
                             1.8.0                   py33_p0  defaults        [mkl]
                             1.8.0                    py33_0  defaults        
                             1.8.0                   py27_p0  defaults        [mkl]
                          *  1.8.0                    py27_0  defaults        
                             1.8.0                   py26_p0  defaults        [mkl]
                             1.8.0                    py26_0  defaults        
                             1.8.1                    py34_0  defaults        
                             1.8.1                   py33_p0  defaults        [mkl]
                             1.8.1                    py33_0  defaults        
                             1.8.1                   py27_p0  defaults        [mkl]
                             1.8.1                    py27_0  defaults        
                             1.8.1                   py26_p0  defaults        [mkl]
                             1.8.1                    py26_0  defaults        
numpydoc                     0.4                      py27_0  defaults        
                             0.4                      py26_0  defaults        

We see there are packages for both versions of NumPy. Let’s keep things simple and create environments with the anaconda meta-package (which will install lots of packages in one go), but simply specify the version of NumPy we want in each.
Let’s create an environment with NumPy 1.6 (click on the commands to expand their output):
In [12]:
!conda create -h
usage: conda-script.py create [-h] [--yes] [--dry-run] [-f] [--file FILE]
                              [--known] [--no-deps] [-m] [--use-index-cache]
                              [--use-local] [-c CHANNEL] [--override-channels]
                              [-n NAME | -p PATH] [-q] [--clone ENV]
                              [--no-default-packages]
                              [package_spec [package_spec ...]]

Create a new conda environment from a list of specified packages. To use the created environment, use 'source activate envname' look in that directory first.  This command requires either the -n NAME or -p PREFIX option.

positional arguments:
  package_spec          package versions to install into conda environment

optional arguments:
  -h, --help            show this help message and exit
  --yes                 do not ask for confirmation
  --dry-run             only display what would have been done
  -f, --force           force install (even when package already installed),
                        implies --no-deps
  --file FILE           read package versions from FILE
  --known               only use index metadata from the known remote
                        channels, and not the local package cache (which are
                        from unknown channels)
  --no-deps             do not install dependencies
  -m, --mkdir           create prefix directory if necessary
  --use-index-cache     use cache of channel index files
  --use-local           use locally built packages
  -c CHANNEL, --channel CHANNEL
                        additional channel to search for packages. These are
                        URLs searched in the order they are given (including
                        file:// for local directories). Then, the defaults or
                        channels from .condarc are searched (unless
                        --override-channels is given). You can use 'defaults'
                        to get the default packages for conda, and 'system' to
                        get the system packages, which also takes .condarc
                        into account. You can also use any name and the
                        .condarc channel_alias value will be prepended. The
                        default channel_alias is http://conda.binstar.org/
  --override-channels   Do not search default or .condarc channels. Requires
                        --channel.
  -n NAME, --name NAME  name of environment (in C:\Users\kiss\Anaconda\envs)
  -p PATH, --prefix PATH
                        full path to environment prefix (default:
                        C:\Users\kiss\Anaconda)
  -q, --quiet           do not display progress bar
  --clone ENV           path to (or name of) existing local environment
  --no-default-packages
                        ignore create_default_packages in condarc file

examples:
    conda create -n myenv sqlite

In [*]:
# При запуске из notebook вылетает "Kernel busy"
# запускать надо из консоли
!conda create -n np1.7 anaconda numpy=1.7
In []:
C:\Users\kiss\Documents\IPython Notebooks\web\for_notebook_editing>conda create -n np1.7 anaconda numpy=1
Fetching package metadata: ..
Solving package specifications: ...................................
Package plan for installation in environment C:\Users\kiss\Anaconda\envs\np1.7:

The following packages will be linked:

    package                    |            build
    ---------------------------|-----------------
    _license-1.1               |           py27_0   hard-link
    anaconda-1.8.0             |       np17py27_0   hard-link
    apptools-4.2.0             |           py27_0   hard-link
    astropy-0.2.5              |       np17py27_0   hard-link
    atom-0.3.4                 |           py27_0   hard-link
    beautiful-soup-4.3.1       |           py27_0   hard-link
    binstar-0.3.1              |           py27_1   hard-link
    biopython-1.62             |       np17py27_0   hard-link
    bitarray-0.8.1             |           py27_0   hard-link
    blaze-0.3                  |       np17py27_0   hard-link
    bokeh-0.2                  |       np17py27_1   hard-link
    boto-2.15.0                |           py27_0   hard-link
    casuarius-1.1              |           py27_0   hard-link
    cdecimal-2.3               |           py27_1   hard-link
    chaco-4.3.0                |       np17py27_1   hard-link
    colorama-0.2.7             |           py27_0   hard-link
    configobj-4.7.2            |           py27_0   hard-link
    cubes-0.10.2               |           py27_3   hard-link
    cython-0.19.2              |           py27_0   hard-link
    dateutil-2.1               |           py27_2   hard-link
    distribute-0.6.45          |           py27_0   hard-link
    docutils-0.11              |           py27_0   hard-link
    dynd-python-0.5.0          |       np17py27_0   hard-link
    enable-4.3.0               |       np17py27_0   hard-link
    enaml-0.8.3                |           py27_0   hard-link
    envisage-4.3.0             |           py27_0   hard-link
    flask-0.10.1               |           py27_1   hard-link
    gevent-0.13.8              |           py27_0   hard-link
    gevent-websocket-0.3.6     |           py27_2   hard-link
    gevent_zeromq-0.2.5        |           py27_2   hard-link
    greenlet-0.4.1             |           py27_0   hard-link
    grin-1.2.1                 |           py27_2   hard-link
    h5py-2.2.0                 |       np17py27_0   hard-link
    imaging-1.1.7              |           py27_2   hard-link
    ipython-1.1.0              |           py27_0   hard-link
    ipython-notebook-1.1.0     |           py27_0   hard-link
    ipython-qtconsole-1.1.0    |           py27_0   hard-link
    itsdangerous-0.23          |           py27_0   hard-link
    jinja2-2.7.1               |           py27_0   hard-link
    keyring-3.2                |           py27_0   hard-link
    launcher-0.1.2             |           py27_0   hard-link
    libpython-1.0              |           py27_0   hard-link
    llvmmath-0.1.1             |       np17py27_2   hard-link
    llvmpy-0.12.0              |           py27_0   hard-link
    lxml-3.2.3                 |           py27_0   hard-link
    markupsafe-0.18            |           py27_0   hard-link
    matplotlib-1.3.1           |       np17py27_0   hard-link
    mayavi-4.3.0               |       np17py27_2   hard-link
    mdp-3.3                    |       np17py27_0   hard-link
    menuinst-1.0.3             |           py27_0   hard-link
    meta-0.4.2.dev             |           py27_0   hard-link
    mingw-4.7                  |                1   hard-link
    networkx-1.8.1             |           py27_0   hard-link
    nltk-2.0.4                 |       np17py27_0   hard-link
    nose-1.3.0                 |           py27_1   hard-link
    numba-0.11.0               |       np17py27_0   hard-link
    numexpr-2.2.2              |       np17py27_0   hard-link
    numpy-1.7.1                |           py27_1   hard-link
    openpyxl-1.6.2             |           py27_0   hard-link
    pandas-0.12.0              |       np17py27_0   hard-link
    patsy-0.2.1                |       np17py27_0   hard-link
    pep8-1.4.6                 |           py27_0   hard-link
    pip-1.4.1                  |           py27_0   hard-link
    ply-3.4                    |           py27_0   hard-link
    psutil-1.1.2               |           py27_0   hard-link
    py-1.4.17                  |           py27_0   hard-link
    pycosat-0.6.0              |           py27_0   hard-link
    pycparser-2.9.1            |           py27_0   hard-link
    pycrypto-2.6.1             |           py27_0   hard-link
    pyface-4.3.0               |           py27_0   hard-link
    pyflakes-0.7.3             |           py27_0   hard-link
    pygments-1.6               |           py27_0   hard-link
    pykit-0.1.0                |       np17py27_0   hard-link
    pyparsing-1.5.6            |           py27_0   hard-link
    pyreadline-2.0.dev         |           py27_0   hard-link
    pysal-1.6.0                |       np17py27_1   hard-link
    pyside-1.2.1               |           py27_0   hard-link
    pytables-3.0.0             |       np17py27_1   hard-link
    pytest-2.4.2               |           py27_0   hard-link
    python-2.7.5               |                2   hard-link
    pytz-2013b                 |           py27_0   hard-link
    pywin32-218.4              |           py27_0   hard-link
    pyyaml-3.10                |           py27_0   hard-link
    pyzmq-2.2.0.1              |           py27_0   hard-link
    requests-1.2.3             |           py27_0   hard-link
    rope-0.9.4                 |           py27_0   hard-link
    scikit-image-0.9.3         |       np17py27_0   hard-link
    scikit-learn-0.14.1        |       np17py27_0   hard-link
    scipy-0.13.0               |       np17py27_0   hard-link
    six-1.4.1                  |           py27_0   hard-link
    sphinx-1.1.3               |           py27_4   hard-link
    spyder-2.2.5               |           py27_0   hard-link
    spyder-app-2.2.5           |           py27_0   hard-link
    sqlalchemy-0.8.3           |           py27_0   hard-link
    statsmodels-0.5.0          |       np17py27_0   hard-link
    sympy-0.7.3                |           py27_0   hard-link
    tornado-3.1.1              |           py27_0   hard-link
    traits-4.3.0               |           py27_0   hard-link
    traitsui-4.3.0             |           py27_0   hard-link
    vtk-5.10.1                 |           py27_0   hard-link
    werkzeug-0.9.4             |           py27_0   hard-link
    xlrd-0.9.2                 |           py27_0   hard-link
    xlwt-0.7.5                 |           py27_0   hard-link

WARNING: the process ipython notebook (11192) is running
WARNING: the process C:\Users\kiss\Anaconda\python.exe C:\Users\kiss\Anaconda\Scripts\ipython-script.py n
WARNING: the process C:\Users\kiss\Anaconda\python.exe -c from IPython.kernel.zmq.kernelapp import main;
ython\profile_default\security\kernel-fec9052d-f6be-4eb6-88d1-a1747e1c0eed.json --IPKernelApp.parent_appn
ofile-dir C:\Users\kiss\.ipython\profile_default --interrupt=1156 --parent=1152 (4264) is running
WARNING: the process C:\Users\kiss\Anaconda\Scripts\conda.bat\..\..\python.exe C:\Users\kiss\Anaconda\Scr
 -n np1.7 anaconda numpy=1.7 (2028) is running
WARNING: Continuing installation while the above processes are running is
not recommended.  Please, close all Anaconda programs before installing or
updating things with conda.

Continue (yes/no/force) (y/[n]/f)?
Естественно, процесс создания я прервал, а информацию из консоли скопировал сюда. А в перовисточнике автор создает целых два виртуальных окружения и они оражаются в списке по команде:
In [1]:
!conda info -e
# conda environments:
#
root                  *  C:\Users\kiss\Anaconda


To use the python version together with all the packages installed in a given environment, simply run the python executable form that environment. From a bash shell:
In []:
# Linux:
~/anaconda/envs/myenv/bin/python
#windows:
 C:\Anaconda\envs\myenv\python.exe

If we’d like to make one of these environments the “default”, we simply need to set our PATH appropriately. From a bash shell:

In []:
# Linux:
$ export PATH=~/anaconda/envs/myenv/bin:$PATH
# Windows:
set PATH=C:\Anaconda\envs\myenv\Scripts;%PATH%
Sometimes we don’t want to create environments with all the packages that the anaconda meta-package brings in. Maybe we want to do some testing in a minimal environment, and conda lets us create those, too.
Let’s say we want to create an environment with scikit-learn and its dependencies, but nothing else. First, let’s see what versions of scikit-learn are available:
In [1]:
!conda search  scikit-learn
Fetching package metadata: ..
scikit-learn                 0.11                 np16py27_0  defaults        
                             0.12.1               np16py27_0  defaults        
                             0.13                 np17py27_0  defaults        
                             0.13                 np17py26_0  defaults        
                             0.13                 np16py27_0  defaults        
                             0.13                 np16py26_0  defaults        
                             0.13.1              np17py27_p0  defaults        [mkl]
                             0.13.1               np17py27_0  defaults        
                             0.13.1               np17py26_0  defaults        
                             0.13.1              np16py27_p0  defaults        [mkl]
                             0.14.1               np17py33_0  defaults        
                             0.14.1              np17py27_p0  defaults        [mkl]
                             0.14.1               np17py27_0  defaults        
                             0.14.1               np17py26_0  defaults        
                             0.14.1              np16py27_p0  defaults        [mkl]
                             0.14.1               np17py33_1  defaults        
                             0.14.1              np17py27_p1  defaults        [mkl]
                             0.14.1               np17py27_1  defaults        
                             0.14.1               np17py26_1  defaults        
                             0.14.1               np18py33_2  defaults        
                             0.14.1              np18py27_p2  defaults        [mkl]
                          *  0.14.1               np18py27_2  defaults        
                             0.14.1               np18py26_2  defaults        
                             0.14.1               np17py33_2  defaults        
                             0.14.1              np17py27_p2  defaults        [mkl]
                             0.14.1               np17py27_2  defaults        
                             0.14.1               np17py26_2  defaults        

There are quite a few! By default, conda will install the latest compatible version, so we will just do that. But before we create an environment, let’s take a look at what the dependencies of scikit-learn are:
In [2]:
!conda depends scikit-learn
Error: Could not locate 'conda-depends'

In [4]:
!conda -h
usage: conda-script.py [-h] [-V] command ...

conda is a tool for managing environments and packages.

positional arguments:
  command
    info         Display information about current conda install.
    help         Displays a list of available conda commands and their help
                 strings.
    list         List linked packages in a conda environment.
    search       Search for packages and display their information. The input
                 is a regular expression. To perform a search with a search
                 string that starts with a -, separate the search from the
                 options with --, like 'conda search -- -h'.
    create       Create a new conda environment from a list of specified
                 packages.
    install      Install a list of packages into a specified conda
                 environment.
    update       Update conda packages.
    remove       Remove a list of packages from a specified conda environment.
    config       Modify configuration values in .condarc. This is modeled
                 after the git config command. Writes to the user .condarc
                 file (C:\Users\kiss\.condarc) by default.
    init         Initialize conda into a regular environment (when conda was
                 installed as a Python package, e.g. using pip).
    clean        Remove unused packages and caches
    package      Low-level conda package utility. (EXPERIMENTAL)
    bundle       Create or extract a "bundle package" (EXPERIMENTAL)

optional arguments:
  -h, --help     show this help message and exit
  -V, --version  show program's version number and exit

external commands:

We don’t need to specify all these dependencies ourselves, we’ll let conda do that work:
In []:
!conda create -n test scikit-learn
Как видно, ко команд уже изменился. Далее в статье речь пошла о создании и подключении собственных репозиториев. Но с предупреждением о том, что материал устарел и надо смотреть новый пост this newer conda blog post


Посты чуть ниже также могут вас заинтересовать

Комментариев нет:

Отправить комментарий