Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import an Anaconda environment .yml in virtualenv?

I just need to import an Anaconda .yml environmental file in virtualenv virtual environment.

The reason I need to do this is because on nVidia Jetson TX2 developer board I cannot install and run Anaconda distribution (It is not compatible with ARM architecture). Virtualenv and Jupyter, instead, are installed and run flawlessly.

The .yml file is listed like this:

name: tfdeeplearning
channels:
  - defaults
dependencies:
  - bleach=1.5.0=py35_0
  - certifi=2016.2.28=py35_0
  - colorama=0.3.9=py35_0
  - cycler=0.10.0=py35_0
  - decorator=4.1.2=py35_0
  - entrypoints=0.2.3=py35_0
  - html5lib=0.9999999=py35_0
  - icu=57.1=vc14_0
  - ipykernel=4.6.1=py35_0
  - ipython=6.1.0=py35_0
  - ipython_genutils=0.2.0=py35_0
  - ipywidgets=6.0.0=py35_0
  - jedi=0.10.2=py35_2
  - jinja2=2.9.6=py35_0
  - jpeg=9b=vc14_0
  - jsonschema=2.6.0=py35_0
  - jupyter=1.0.0=py35_3
  - jupyter_client=5.1.0=py35_0
  - jupyter_console=5.2.0=py35_0
  - jupyter_core=4.3.0=py35_0
  - libpng=1.6.30=vc14_1
  - markupsafe=1.0=py35_0
  - matplotlib=2.0.2=np113py35_0
  - mistune=0.7.4=py35_0
  - mkl=2017.0.3=0
  - nbconvert=5.2.1=py35_0
  - nbformat=4.4.0=py35_0
  - notebook=5.0.0=py35_0
  - numpy=1.13.1=py35_0
  - openssl=1.0.2l=vc14_0
  - pandas=0.20.3=py35_0
  - pandocfilters=1.4.2=py35_0
  - path.py=10.3.1=py35_0
  - pickleshare=0.7.4=py35_0
  - pip=9.0.1=py35_1
  - prompt_toolkit=1.0.15=py35_0
  - pygments=2.2.0=py35_0
  - pyparsing=2.2.0=py35_0
  - pyqt=5.6.0=py35_2
  - python=3.5.4=0
  - python-dateutil=2.6.1=py35_0
  - pytz=2017.2=py35_0
  - pyzmq=16.0.2=py35_0
  - qt=5.6.2=vc14_6
  - qtconsole=4.3.1=py35_0
  - requests=2.14.2=py35_0
  - scikit-learn=0.19.0=np113py35_0
  - scipy=0.19.1=np113py35_0
  - setuptools=36.4.0=py35_1
  - simplegeneric=0.8.1=py35_1
  - sip=4.18=py35_0
  - six=1.10.0=py35_1
  - testpath=0.3.1=py35_0
  - tk=8.5.18=vc14_0
  - tornado=4.5.2=py35_0
  - traitlets=4.3.2=py35_0
  - vs2015_runtime=14.0.25420=0
  - wcwidth=0.1.7=py35_0
  - wheel=0.29.0=py35_0
  - widgetsnbextension=3.0.2=py35_0
  - win_unicode_console=0.5=py35_0
  - wincertstore=0.2=py35_0
  - zlib=1.2.11=vc14_0
- pip:
  - ipython-genutils==0.2.0
  - jupyter-client==5.1.0
  - jupyter-console==5.2.0
  - jupyter-core==4.3.0
  - markdown==2.6.9
  - prompt-toolkit==1.0.15
  - protobuf==3.4.0
  - tensorflow==1.3.0
  - tensorflow-tensorboard==0.1.6
  - werkzeug==0.12.2
  - win-unicode-console==0.5
prefix: C:\Users\Marcial\Anaconda3\envs\tfdeeplearning
like image 613
capocchione Avatar asked Mar 05 '23 05:03

capocchione


1 Answers

pip can install from a requirements.txt file, which would look like the items in the sequence that is the value for the key pip in your .yml file, but without the dashes:

ipython-genutils==0.2.0
jupyter-client==5.1.0
jupyter-console==5.2.0
jupyter-core==4.3.0
markdown==2.6.9
prompt-toolkit==1.0.15
protobuf==3.4.0
tensorflow==1.3.0
tensorflow-tensorboard==0.1.6
werkzeug==0.12.2
win-unicode-console==0.5

Assuming that the end of your file actually looks like:

  .
  .
  .
  - wincertstore=0.2=py35_0
  - zlib=1.2.11=vc14_0
  - pip:
    - ipython-genutils==0.2.0
    - jupyter-client==5.1.0
    - jupyter-console==5.2.0
    - jupyter-core==4.3.0
    - markdown==2.6.9
    - prompt-toolkit==1.0.15
    - protobuf==3.4.0
    - tensorflow==1.3.0
    - tensorflow-tensorboard==0.1.6
    - werkzeug==0.12.2
    - win-unicode-console==0.5
prefix: C:\Users\Marcial\Anaconda3\envs\tfdeeplearning

(i.e. the entry for pip is indented to make this a valid YAML file), and is named anaconda-project.yml, you can do:

import ruamel.yaml

yaml = ruamel.yaml.YAML()
data = yaml.load(open('anaconda-project.yml'))

requirements = []
for dep in data['dependencies']:
    if isinstance(dep, str):
        package, package_version, python_version = dep.split('=')
        if python_version == '0':
            continue
        requirements.append(package + '==' + package_version)
    elif isinstance(dep, dict):
        for preq in dep.get('pip', []):
            requirements.append(preq)

with open('requirements.txt', 'w') as fp:
    for requirement in requirements:
       print(requirement, file=fp)

resulting in a requirement.txt file, which can be used with:

pip install -r requirements.txt

Please note:

  • the non-pip packages might not be available from PyPI

  • the current pip version is 18.1, the one in that requirements list is old

  • that according to the official YAML FAQ, using .yml as an extension for your YAML file should only be done if the recommended .yaml extension. On modern filesystems that is never the case. I don't know if Anaconda is, as so often, non-conform, or that you have a choice in the matter.

  • since the introduction of binary wheels a few years ago, and many packages supporting them, it is often (and for me always) possible to just use virtualenvs and pip. And thereby circumventing the problems caused by Anaconda not being 100% compliant and not being up-to-date with all its packages (compared to PyPI).

like image 193
Anthon Avatar answered Mar 08 '23 23:03

Anthon