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.

jub
  • 377
  • 1
  • 2
  • 14
  • 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. – Sin Han Jinn Nov 23 '22 at 02: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. – jub Nov 24 '22 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 >> https://stackoverflow.com/a/63064925/12128167 – Sin Han Jinn Nov 29 '22 at 03: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. – jub Dec 03 '22 at 01:49
  • I can't debug without seeing script 2. Are you using configparser() function? – Sin Han Jinn Dec 05 '22 at 01:20
  • @SinHanJinn script 2 is a somewhat long script, but the issue isn't the script. Configparser is being used by the script and it is one of the first packages imported by the script. Configparser was installed in the virtual environment, and the package files are here: D:\coding_projects\myProj1\proj_env\ The issue seems to be that running ```subprocess.run(["D:\coding_projects\myProj1\proj_env\Scripts\python.exe"...``` is not sufficient to activate the virtual environment, so python is looking to the system installation's environment for configparser and not finding it. – jub Dec 05 '22 at 21:42
  • You can try using Popen > https://stackoverflow.com/a/27123973/12128167 Since we are drifting of the question, you may open a new question if you are still facing difficulties. – Sin Han Jinn Dec 06 '22 at 00:55

0 Answers0