如何从anaconda虚拟环境(而不是从基本环境)执行批处理脚本?

时间:2020-06-14 20:35:13

标签: batch-file pip anaconda conda virtual-environment

我想在Conda环境中运行批处理文件,而不是在 base 环境中,而是在另一个虚拟环境(此处为 pylayers )中运行。 我将activate.bat脚本从F:\Anaconda3\Scripts复制到了F:\Anaconda3\envs\pylayers\Scripts

我的批处理脚本(installer_win.bat)是:

call F:\Anaconda3\envs\pylayers\Scripts\activate.bat
conda install numpy --yes
conda install mayavi --yes
conda install shapely --yes
conda install libgdal --yes
conda install gdal --yes
conda install h5py --yes
conda install seaborn --yes
conda install PIL --yes
conda install basemap --yes
conda install wxpython --yes
conda install netCDF4 --yes
pip install protobuf 
pip install tqdm
pip install descartes
pip install bitstring
pip install geocoder
pip install triangle
pip install osmapi
pip install pyshp 
pip install Image
pip install pathos
pip install SimPy==2.2
pip install simplekml
pip install smopy

python setup.py install

当我执行installer_win.bat文件时,它显示以下行为:

Output: #stops after executing very first line in the batch file.
(base) C:\Users\mkdth>activate pylayers

(pylayers) C:\Users\mkdth>cd /d F:\Pycharm\Projects\pylayers-master

(pylayers) F:\Pycharm\Projects\pylayers-master>installer_win.bat

(pylayers) F:\Pycharm\Projects\pylayers-master>call F:\Anaconda3\envs\pylayers\Scripts\activate.bat
The system cannot find the path specified.

(pylayers) F:\Pycharm\Projects\pylayers-master>conda install numpy --yes
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: F:\Anaconda3\envs\pylayers

  added / updated specs:
    - numpy


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    openssl-1.1.1g             |       he774522_0         4.8 MB
    ------------------------------------------------------------
                                           Total:         4.8 MB

The following NEW packages will be INSTALLED:

  numpy-base         pkgs/main/win-64::numpy-base-1.18.1-py36hc3f5095_1

The following packages will be SUPERSEDED by a higher-priority channel:

  ca-certificates    conda-forge::ca-certificates-2020.4.5~ --> pkgs/main::ca-certificates-2020.1.1-0
  certifi            conda-forge::certifi-2020.4.5.2-py36h~ --> pkgs/main::certifi-2020.4.5.1-py36_0
  numpy              conda-forge::numpy-1.18.5-py36h4d86e3~ --> pkgs/main::numpy-1.18.1-py36h93ca92e_0
  openssl                                       conda-forge --> pkgs/main



Downloading and Extracting Packages
openssl-1.1.1g       | 4.8 MB    | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

(pylayers) F:\Pycharm\Projects\pylayers-master>

我还尝试修改批处理文件以从 base env激活 pylayers 环境,并逐个运行condapip命令一个,但仅安装在 base 环境中。查看蝙蝠脚本

尝试1

Installer_win.bat文件:

call F:\Anaconda3\Scripts\activate.bat
activate pylayers
pause
conda install numpy --yes
conda install mayavi --yes
conda install shapely --yes
conda install libgdal --yes
conda install gdal --yes
conda install h5py --yes
conda install seaborn --yes
conda install PIL --yes
conda install basemap --yes
conda install wxpython --yes
conda install netCDF4 --yes
pip install protobuf 
pip install tqdm
pip install descartes
pip install bitstring
pip install geocoder
pip install triangle
pip install osmapi
pip install pyshp 
pip install Image
pip install pathos
pip install SimPy==2.2
pip install simplekml
pip install smopy

python setup.py install

输出:激活pylayers env并停止

(pylayers) F:\Pycharm\Projects\pylayers-master>installer_win.bat

(pylayers) F:\Pycharm\Projects\pylayers-master>call F:\Anaconda3\Scripts\activate.bat

(base) F:\Pycharm\Projects\pylayers-master>activate pylayers

(pylayers) F:\Pycharm\Projects\pylayers-master>

Attempt2:

Installer_win.bat文件:

call F:\Anaconda3\Scripts\activate.bat
activate pylayers
conda install numpy --yes
conda install mayavi --yes
conda install shapely --yes
conda install libgdal --yes
conda install gdal --yes
conda install h5py --yes
conda install seaborn --yes
conda install PIL --yes
conda install basemap --yes
conda install wxpython --yes
conda install netCDF4 --yes
pip install protobuf 
pip install tqdm
pip install descartes
pip install bitstring
pip install geocoder
pip install triangle
pip install osmapi
pip install pyshp 
pip install Image
pip install pathos
pip install SimPy==2.2
pip install simplekml
pip install smopy

python setup.py install

输出:#激活基本环境并停止

(pylayers) F:\Pycharm\Projects\pylayers-master>installer_win.bat

(pylayers) F:\Pycharm\Projects\pylayers-master>call F:\Anaconda3\Scripts\activate.bat

(base) F:\Pycharm\Projects\pylayers-master>activate pylayers

尝试3

Installer_win.bat文件:

call F:\Anaconda3\Scripts\activate.bat
conda install numpy --yes
conda install mayavi --yes
conda install shapely --yes
conda install libgdal --yes
conda install gdal --yes
conda install h5py --yes
conda install seaborn --yes
conda install PIL --yes
conda install basemap --yes
conda install wxpython --yes
conda install netCDF4 --yes
pip install protobuf 
pip install tqdm
pip install descartes
pip install bitstring
pip install geocoder
pip install triangle
pip install osmapi
pip install pyshp 
pip install Image
pip install pathos
pip install SimPy==2.2
pip install simplekml
pip install smopy

python setup.py install

输出:#开始在基本环境中安装

(pylayers) F:\Pycharm\Projects\pylayers-master>installer_win.bat

(pylayers) F:\Pycharm\Projects\pylayers-master>call F:\Anaconda3\Scripts\activate.bat

(base) F:\Pycharm\Projects\pylayers-master>activate pylayers

(pylayers) F:\Pycharm\Projects\pylayers-master>installer_win.bat

(pylayers) F:\Pycharm\Projects\pylayers-master>call F:\Anaconda3\Scripts\activate.bat

(base) F:\Pycharm\Projects\pylayers-master>conda install numpy --yes
Collecting package metadata (current_repodata.json): failed

CondaError: KeyboardInterrupt

^CTerminate batch job (Y/N)?
^C

请问有人可以帮助我在Conda虚拟环境下运行此批处理文件吗?

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:2)

有两种方法。我认为第一种是更清洁的方法。

选项1:YAML定义

如果整个过程仅用于安装,则可以将其压缩为一个YAML environment definition并一次性执行。这包括本地软件包安装,对于当前示例,我将其称为.form .input label { top: -25px /*or any value you want*/ }

foo_install.yaml

foo

要将其作为对环境的添加安装到任意环境中(例如name: foo_install channels: - defaults dependencies: - numpy - mayavi - shapely - libgdal - gdal - h5py - seaborn - PIL - basemap - wxpython - netCDF4 - pip - pip: - protobuf - tqdm - descartes - bitstring - geocoder - triangle - osmapi - pyshp - Image - pathos - SimPy==2.2 - simplekml - smopy - -e ./../foo # this assumes running from inside the `foo` folder ),可以使用

pylevels

此文件在conda env update -n pylevels -f foo_install.yaml 文件夹中运行。

有关其他选项的展示,请参见the Advanced Pip Example(基本上,foo接受的所有内容都可以包含在YAML文件中)。

选项2:pip install

更一般地说,可以使用conda run在特定环境中运行任意脚本。这样避免了必须在脚本本身中包含激活命令。在这种情况下,您可以使用

运行脚本
conda run

如果您还需要配置一些环境变量或类似的东西,我只会求助于此。在那种情况下,我仍然会制作YAML,并将所有安装减少到一行:

installer_win.bat

conda run -n pylevels installer_win.bat

,并在此之后添加其他动作。

答案 1 :(得分:0)

要在新的(非基础)conda env中的dos提示符下运行bat文件,您可以尝试执行以下操作:

提示> cmd“ / c激活ds_tensorflow && myfile.bat &&停用”

myfile.bat的内容以显示您位于非基础环境中:

echo hello
python -c "import sys; print(sys.version)"

您可以将上面>>行中的myfile.bat替换为bat文件。在没有bat文件的情况下也可以使用,只需在/ c activate ....停用包装器中运行每个命令即可。

cmd "/c activate ds_tensorflow && python -c "import sys; print(sys.version)" && deactivate"