春江暮客

春江暮客的个人学习分享网站

Running Conda Command-Line Scripts on Windows

2019-05-17 Miscellaneous
Running Conda Command-Line Scripts on Windows

I needed to remotely update website content from my Windows machine at home, and while I have Conda installed on Windows, I found I couldn’t directly write and run scripts. I could only launch Conda from the Start menu and type commands line by line.

After some searching and learning, I finally found a solution.

The reason you can’t directly write and run .bat batch files for Conda commands (which use various Python libraries from Conda) is that the default cmd.exe doesn’t activate the Conda environment. Once you add the environment activation command, your scripts will run. You can refer to the original English explanation here: https://stackoverflow.com/questions/46305569/how-to-make-batch-files-run-in-anaconda-prompt.

The final batch-file structure can look like this:

call "G:\conda\Scripts\activate.bat"
call conda activate base
scrapy crawl dou
python tupian.py
python post.py

If you want to run the script inside a specific environment instead of base, just replace the environment name:

call conda activate myenv

If the batch file still says that conda or python cannot be found, check these first:

  • Whether the activate.bat path matches your real Anaconda or Miniconda installation path
  • Whether the batch file is being run in a normal cmd context rather than through another tool that rewrites environment variables

友情链接

其它