0

I have a python script ('script #1') that runs in a Windows CMD window all day long. The script runs a pynput keyboard listener, and I use this to execute a defined function in the script via a hotkey.

Can I use this same script to add/listen for a hotkey and then launch a different virtual environment and execute a separate python file ('script #2')? Any pointers on how this could be implemented?

Or should I instead add a keyboard listener to script #2 and then dump everything except the import statements of script #2 inside a def functionName block, and then run this second listener script in a separate CMD window? Seems kind of inefficient to do this way, especially as I'd ideally like to have a number of python scripts execute via hotkey.

7
  • May I know what does your script do, as in what's in your function? do you really need a new virtual environment or you want both scripts to be running on the same environment? I would highly suggest for you to explore multithreading in python if you want to run few tasks at a time, instead of executing different scripts for it. Commented Nov 23, 2022 at 2:40
  • @SinHanJinn Script #1 has a keyboard listener, and I use it to copy text, manipulate it and paste into a particular app. I run this as needed, usually multiple times/day. Script #2 uses a different virtual environment than Script #1, though I could install the necessary packages to Script #1's virtual env. Script #2 gets run a few times/day, and I currently run it from VS Code; just looking for a more convenient method of executing the script than having to switch to VS Code window, selecting file and running it. Commented Nov 24, 2022 at 17:19
  • Since you already have script 1 running to detect your keyboard inputs, I think it's redundant for script 2 to do the same. You could try to call script 2 from script 1 under it's own environment. See if this method helps >> stackoverflow.com/a/63064925/12128167 Commented Nov 29, 2022 at 3:51
  • So I tried including this in script 1 (function 2 is activated by a hotkey that script 1 is listening for): def function_2():, subprocess.run(["D:\coding_projects\myProj1\proj_env\Scripts\python.exe", "-c", "print('test using venv python')"]), this works fine. But when I change definition of function 2 to: subprocess.run(["D:\coding_projects\myProj1\proj_env\Scripts\python.exe", "D:\coding_projects\myProj1\test_script.py"]), I get a configparser error and I can see the configparser.py that is called is from system installation of Python, not the venv python. Commented Dec 3, 2022 at 1:49
  • I can't debug without seeing script 2. Are you using configparser() function? Commented Dec 5, 2022 at 1:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.