如何在virtualenv中导入Anaconda环境.yml? - python

我只需要在virtualenv虚拟环境中导入Anaconda .yml环境文件。

我需要这样做的原因是因为在nVidia Jetson TX2开发板上,我无法安装和运行Anaconda发行版(它与ARM架构不兼容)。相反,Virtualenv和Jupyter可以完美安装并运行。

.yml文件列出如下:

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

python参考方案

pip可以从requirements.txt文件安装,该文件看起来像
序列中作为键值的项目
pip文件中的.yml,但没有破折号:

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

假设文件的末尾实际上如下所示:

  .
  .
  .
  - 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

(即缩进pip的条目以使其成为有效的YAML文件),
并命名为anaconda-project.yml,您可以执行以下操作:

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)

生成一个requirement.txt文件,该文件可用于:

pip install -r requirements.txt

请注意:

非pip包可能无法从PyPI获得
当前的pip版本为18.1,该要求列表中的版本为旧
根据官方的YAML常见问题解答,使用.yml作为
仅在以下情况下,才应为YAML文件扩展名
.yaml扩展名。在现代文件系统上,情况并非如此。一世
不知道Anaconda是否经常不合格,或者您
在这件事上有选择。
自从几年前引入二元轮以来,
支持它们的软件包,通常(对我而言)总是可能的
只使用virtualenvs和pip。从而规避了
由Anaconda引起的问题不是100%兼容且不符合
与其所有软件包保持最新(与PyPI相比)。

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…

Python:检查新文件是否在文件夹中[重复] - python

This question already has answers here: How do I watch a file for changes? (23个答案) 3年前关闭。 我是python的新手,但是我尝试创建一个自动化过程,其中我的代码将侦听目录中的新文件条目。例如,某人可以手动将zip文件复制到一个特定的文件夹中,并且我希望我的代码能够在文件完全…