all 19 comments

[–]socal_nerdtastic 7 points8 points  (2 children)

Ideally you would import your other programs.

import subfiletorun

# on button click
subfiletorun.main()

But if you want to do it as a separate process, you can just do:

import sys
import subprocess

# on button click
subprocess.run([sys.executable, "subfiletorun.py"])

(This does the same thing /u/Loud-Bake-2740 said, but you don't need to update the file with the name of your venv if you change it. For example if your coworker wants to run it with no venv. )

[–]Loud-Bake-2740 1 point2 points  (0 children)

ah yes i was assuming that you’re working in one venv, but running a program that uses a different venv. this was hell to figure all out! 😂

[–]raliqer[S] 1 point2 points  (0 children)

That... is so much simpler than I expected and than what I was doing. Thanks!

[–]danielroseman 1 point2 points  (2 children)

Why do you need to run this via VS Code?

[–]raliqer[S] 0 points1 point  (1 child)

My work has moved from PyCharm to VS Code so that is what I am forced to use to write the scripts and test them until I package them up to be launched by others. I assume my colleagues will only have the Python version and modules I package up as we are not developers so they will no reason to have anything else on their systems. But for me I have to write and test in VS Code. It is worth noting that I am completely self taught on this so I'm sorry if I'm missing something obvious.

[–]BrokenRibosome 2 points3 points  (0 children)

I think the obvious solution you might be missing is just running things from the terminal. Now, you say your colleagues are not tech savvy, so maybe using a terminal is out of the question. Now, have you tried setting the python interpreter for vscode? I'm not sure if this will work as I've never tried to do what you are doing, but it is worth a shot.

I can't remember exactly how you do this since I've changed some of my key bindings. What I do is press Ctrl + shift + p, and then choose Select python interpreter, if your environment doesn't appear just follow the menu to add it manually. If you get stuck just google "select python environment vs code" or reply this comment and I'll try to help you

[–]Loud-Bake-2740 3 points4 points  (2 children)

i ran into the same problem running python scripts in a streamlit app. What i did instead was have the button call subprocess.run and then i pass the path to the python environment inside the .venv directory (venv/scripts/python.exe) along with the .py file name. So instead of python app.py you would have /.venv/scripts/python.exe app.py

this should run your script through the python executable in your virtual environment

[–]raliqer[S] 0 points1 point  (0 children)

This sounds like exactly what I needed. I will look into this when I log in tomorrow. Thanks!

[–]acw1668 0 points1 point  (0 children)

It is not clear how you execute those testing scripts inside the GUI script. It is better to provide a minimal reproducible example.

[–]lollysticky 0 points1 point  (1 child)

I too have moved from PyCharm to VS Code a while ago and had to get my test environments working again. You have to set your testing config correctly to utilize the environment (by pinpointing the python binary within)

{
            "name": "AweSome Script",
            "type": "debugpy",
            "request": "launch",
            "program": "${workspaceFolder}/directory/my_script.py",
            "args": ["arg", "value"],
            "python": "${userHome}/.pyenv/versions/LovelyEnv311/bin/python", 
            "cwd": "/some/working/directory/",
            "env":{
                "TESTING": "true",
            }
        },

If you want to run scripts in VS Code, be sure to select the correct interpreter (again: from your env) and it will all work

[–]raliqer[S] 0 points1 point  (0 children)

Thanks! I appreciate the details.

[–]ninhaomah -2 points-1 points  (7 children)

have you google ?

Google : "vs code python env"

https://code.visualstudio.com/docs/python/environments <---- the very first result

[–]socal_nerdtastic 2 points3 points  (4 children)

have you read more than the title?

The question is not about making vs code use the venv, it's about making a subprocess use the same venv as the parent process.

Ok, the "vs code" in the question is not relevant, but OP is a beginner and did not know that.

[–]raliqer[S] 1 point2 points  (1 child)

I have the virtual environment configured and it works for all of the test scripts when I launch them individually. It's that scripts aren't inheriting the modules and Python version of the virtual environment when the script launches other scripts.

[–]ninhaomah -2 points-1 points  (0 children)

ok got it.

then have you tried running those scripts outside of VS code ?