如何从 anaconda 虚拟环境(而不是从 base)执行批处理脚本?
How to execute a batch script from an anaconda virtual environment (not from base)?
我想在 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 和 运行 conda
或 pip
一一命令,但它仅安装到 base 环境。查看bat脚本
尝试 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>
尝试 2:
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
输出:#activates base env and stops
(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
输出:#starts installing in base 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>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 虚拟环境下 运行 这个批处理文件吗?
如有任何建议,我们将不胜感激。
要 运行 在新的(非基础)conda env 中从 dos 提示符生成 bat 文件,您可以尝试这样的操作:
提示符> cmd "/c 激活 ds_tensorflow && myfile.bat && 停用"
myfile.bat 的内容表明您处于非基础环境中:
echo hello
python -c "import sys; print(sys.version)"
您可以用您的 bat 文件替换上面 >> 行中的 myfile.bat。这也可以在没有 bat 文件的情况下工作,只需 运行 /c activate .... deactivate wrapper 中的每个命令。
cmd "/c activate ds_tensorflow && python -c "import sys; print(sys.version)" && deactivate"
有两条路可以走。我认为第一个是更清洁的方法。
选项 1:YAML 定义
如果整个过程只是安装,可以压缩成一个YAML environment definition一次性执行。这包括本地包安装,对于当前示例,我将其称为 foo
.
foo_install.yaml
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
这是 foo
文件夹中的 运行。
有关其他选项的展示,请参阅 the Advanced Pip Example(基本上,pip install
接受的所有内容都可以包含在 YAML 文件中)。
选项 2:conda run
更一般地说,可以通过使用 conda run
在特定环境中 运行 任意脚本。这避免了必须在脚本本身中包含激活命令。在这种情况下,您可以 运行 您的脚本
conda run -n pylevels installer_win.bat
如果您还需要配置一些环境变量或类似的东西,我只会求助于此。在那种情况下,我仍然会制作 YAML,并将所有安装减少到单行:
installer_win.bat
conda env update -f foo_install.yaml
并包括之后的进一步行动。
我想在 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 和 运行 conda
或 pip
一一命令,但它仅安装到 base 环境。查看bat脚本
尝试 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>
尝试 2:
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
输出:#activates base env and stops
(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
输出:#starts installing in base 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>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 虚拟环境下 运行 这个批处理文件吗?
如有任何建议,我们将不胜感激。
要 运行 在新的(非基础)conda env 中从 dos 提示符生成 bat 文件,您可以尝试这样的操作:
提示符> cmd "/c 激活 ds_tensorflow && myfile.bat && 停用"
myfile.bat 的内容表明您处于非基础环境中:
echo hello
python -c "import sys; print(sys.version)"
您可以用您的 bat 文件替换上面 >> 行中的 myfile.bat。这也可以在没有 bat 文件的情况下工作,只需 运行 /c activate .... deactivate wrapper 中的每个命令。
cmd "/c activate ds_tensorflow && python -c "import sys; print(sys.version)" && deactivate"
有两条路可以走。我认为第一个是更清洁的方法。
选项 1:YAML 定义
如果整个过程只是安装,可以压缩成一个YAML environment definition一次性执行。这包括本地包安装,对于当前示例,我将其称为 foo
.
foo_install.yaml
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
这是 foo
文件夹中的 运行。
有关其他选项的展示,请参阅 the Advanced Pip Example(基本上,pip install
接受的所有内容都可以包含在 YAML 文件中)。
选项 2:conda run
更一般地说,可以通过使用 conda run
在特定环境中 运行 任意脚本。这避免了必须在脚本本身中包含激活命令。在这种情况下,您可以 运行 您的脚本
conda run -n pylevels installer_win.bat
如果您还需要配置一些环境变量或类似的东西,我只会求助于此。在那种情况下,我仍然会制作 YAML,并将所有安装减少到单行:
installer_win.bat
conda env update -f foo_install.yaml
并包括之后的进一步行动。