简体   繁体   中英

Batch that starts python virtual environment and runs the virtual python in a project dir

I want to run each day a python script in a virtual environment by recording it in Windows Task Scheduler. Hence, I need to write a batch that:

  • Opens a cmd.exe
  • Starts a virtual environment
  • Changes the directory to a python project directory
  • Invokes the virtual python

I wrote this .bat:

cmd /k "cd /d %USERPROFILE%\python_venv\venv1\Scripts & activate & cd /d %PROJECT_DIR%\src"
python main.py

Of course cmd /k does not terminate, and python main.py is not executed.

Sorry in advance if the answer already exists over the Internet but I was not able to find it or to modify a close answer to suit my needs.

[EDIT] This question is a duplicate of Schedule [Virtualenv Dependent] Python Script with Windows Task Scheduler . This post has also not been answered yet.

[EDIT 2] Found an answer here: Run a python script in virtual environment from windows task scheduler

This might be completely irrelevant but I've had a great deal of success using PyCharm + virtual environments with python, you set your VM's file structure up as a project and just add the python environment that you'd like to use with it, it's relatively simple:

Requirements:

  • Virtual Env
  • Pycharm

Go to ssh to the virtual env and type which python

Add remote project interpreter (File > Default Settings > Project Interpreter (cog) add remote) and then type in/select the location for the remote python location

You'll need to set up your file system so that PyCharm can also open the project.

NOTE:

Do not turn off your virtual environment without saving your run configurations that will cause pycharm to see your run configurations as corrupt There's a button on the top right that reads share enable this and your run configs will be saved to a .idea file and you'll have a lot less issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM