0

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

  • I found an answer ! _full_path_to_virtualenv_\Scripts\activate.bat && python _full_path_to_python_script_.py – Adrien Faucon Jul 09 '17 at 08:56
  • Possible duplicate of [Run a python script in virtual environment from windows task scheduler](https://stackoverflow.com/questions/34622514/run-a-python-script-in-virtual-environment-from-windows-task-scheduler) – Adrien Faucon Jul 09 '17 at 09:14

1 Answers1

0

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

Tomos Williams
  • 1,988
  • 1
  • 13
  • 20
  • I agree with you. I knew this was possible with pycharm. But in my case, i just wanted to quickly start en virtual environment in a specific directory and start working in it. – Adrien Faucon Jun 27 '17 at 11:37
  • I also want to start my process each day thanks to Windows Task Scheduler. So the PyCharm solution does not work. – Adrien Faucon Jul 09 '17 at 08:46