Reputation: 97
I want to run a batch file in a Conda environment, not in the base env, but in another virtual environment (here pylayers).
I copied the activate.bat
script from F:\Anaconda3\Scripts
to F:\Anaconda3\envs\pylayers\Scripts
.
And my batch script (installer_win.bat
) is:
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
When I execute the installer_win.bat
file, it shows the following behavior:
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>
I also tried to modify the batch file to activate pylayers environment from the base env and run the conda
or pip
commands one by one, but it installs to base environment only. See the bat script
Installer_win.bat
file:
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
Output: activates pylayers 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
(pylayers) F:\Pycharm\Projects\pylayers-master>
Installer_win.bat
file:
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
Output: #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
Installer_win.bat
file:
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
Output: #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
Can someone help me to run this batch file under a Conda virtual env, please?
Any suggestions would be very much appreciated.
Upvotes: 1
Views: 2617
Reputation: 76700
There are two ways to go. I think the first is the cleaner way to go.
If the entire procedure is only for installations, it can be condensed into a single YAML environment definition and executed in a one go. This includes the local package installation, which for the current example I'll call 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
To install this into an arbitrary env (e.g., pylevels
) as an addition to the env, one would use
conda env update -n pylevels -f foo_install.yaml
where this is run from within the foo
folder.
See the Advanced Pip Example for a showcase of other options (basically, everything pip install
accepts can be included in a YAML file).
conda run
More generically, one can run arbitrary scripts inside a specific environment by using conda run
. This avoids having to include activation commands inside scripts themselves. In this case, you could run your script with
conda run -n pylevels installer_win.bat
I would only resort to this if you need to also configure some environment variables or something similar. In that case, I would still make the YAML, and reduce all the installation to the single line:
installer_win.bat
conda env update -f foo_install.yaml
and include further actions after that.
Upvotes: 2
Reputation: 1753
To run a bat file from a dos prompt inside a new (non-base) conda env, you can try something like this:
prompt> cmd "/c activate ds_tensorflow && myfile.bat && deactivate"
contents of myfile.bat to show you are in the non-base env:
echo hello
python -c "import sys; print(sys.version)"
You can replace myfile.bat in the >> line above with your bat file. This will also work without the bat file, just run each command within the /c activate .... deactivate wrapper.
cmd "/c activate ds_tensorflow && python -c "import sys; print(sys.version)" && deactivate"
Upvotes: 0