Python Forum

Full Version: Creating Python scripts in Visual Studio Code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am new to Python programming. I have developed some scripts that read data from ms access database and post the results to firebase firestore database . So far i have created just 3 scripts in Visual Studio Code (without creating any Virtual Environment) and they all run perfectly. My first question is , is it ok to run python scripts without creating a virtual environment ? If No how do i create a virtual environment inside Visual Studio Code? My second question is how can i integrate my frontend html with python on button click ?

Please help

Thanks & regards

Sanjish
You can run python scripts without a virtual environment, but it keeps code having to do with the project isolated, so that you don't have to carry baggage from non related projects.

Virtual environment is made on command line (terminal emulator window), usually from outside of VSCode, but would work from a terminal window inside of VSCode as well.

The python command is python -m venv myvirtualenvname I usually name the virtual environment venv as well, which avoids confusion so then command python -m venv venv.

Once created, you must tell VSCode where the interpreter is located, from a code, do this by typing ctrl-shift-P in VSCode. in the window, type: Python: Select Interpreter
Scroll down until you find an entry with: .venv/bin/python. Your virtual environment will be activated when you run a script.
As mention over make virtual environment from command line.
I always use code . to start VS Code from virtual environment folder or any other folder that want start as root in VS Code.
Then left click down in left cornier or Python: Select Interpreter(ctrl-shift-P),then choose Interpreter of virtual environment or which Interpreter that want to use.
VS Code from start
Quick demo.
# Make virtual environment
G:\div_code
λ python -m venv my_env

# Cd in
G:\div_code
λ cd my_env\

# Activate
G:\div_code\my_env
λ G:\div_code\my_env\Scripts\activate

# Start VS Code
(my_env) G:\div_code\my_env
λ code .
[Image: uAzCsO.png]
Hi ,
Thanks for the info on Virtual Environment. Could you please also help me in Linking html to Python page ?
(Dec-21-2020, 11:09 AM)Sanjish Wrote: [ -> ]My second question is how can i integrate my frontend html with python on button click ?
Thanks for the info on Virtual Environment. Could you please also help me in Linking html to Python page ?
You will need something like eg Flask or Django.
In the old days there was CGI(which now dead in Python),so eg Flask has taken over that role in a more modern and better way using WSGI underneath.
Look at this post for a basic setup with html and css.
There is also webbrowser module,that can be used for displaying html,but has no connection back to Python with eg a button click.