0 %
!
Programmer
SEO-optimizer
English
German
Russian
HTML
CSS
WordPress
Python
C#
  • Bootstrap, Materialize
  • GIT knowledge
0

No products in the cart.

How to Run a Python Script on a PC via a Shortcut

04.02.2025

Creating a Desktop Shortcut for a Python Script

Running a Python script through a desktop shortcut on Windows makes launching your projects faster and more convenient. If you’re working with Streamlit, setting up a shortcut can save you from manually activating a virtual environment each time.


1. Standard Python Script Shortcut

Step 1: Locate Your Python Script

Find the script you want to run and note its full file path. Example:

C:\Users\YourName\Scripts\my_script.py

Step 2: Find Your Python Executable

You need the path to Python, which can be found by running:


where python

Example output:
C:\Users\YourName\AppData\Local\Programs\Python\Python39\python.exe

Step 3: Create a Shortcut

  1. Right-click on the desktop → Select New → Shortcut.
  2. In the location field, enter:
    "C:\path\to\python.exe" "C:\path\to\your_script.py"
    Example:
    "C:\Users\YourName\AppData\Local\Programs\Python\Python39\python.exe" "C:\Users\YourName\Scripts\my_script.py"
  3. Click Next, name the shortcut, and click Finish.

Step 4: Modify Shortcut Properties (Optional)

To run the script in the background, replace python.exe with pythonw.exe.


2. Running a Streamlit App via a Shortcut

If you’re working with Streamlit, you need to activate your virtual environment before running the app. Here’s how to set up a shortcut that does it automatically.

Step 1: Identify Your Virtual Environment Path

Find your virtual environment’s activate.bat script. Example:

F:\PYTHON\STREAMLIT\kinopoisk\.venv\Scripts\activate.bat

Step 2: Find Your Streamlit App Path

Locate your app.py file. Example:

F:\PYTHON\STREAMLIT\kinopoisk\app.py

Step 3: Create a Streamlit Shortcut

  1. Right-click on the desktop → Select New → Shortcut.
  2. In the location field, enter:

    cmd /k "F:\PYTHON\STREAMLIT\kinopoisk\.venv\Scripts\activate.bat && python -m streamlit run F:\PYTHON\STREAMLIT\kinopoisk\app.py"
  3. Click Next, name the shortcut (e.g., Run Streamlit App), and click Finish.

Step 4: Customize the Shortcut Icon (Optional)

  1. Right-click the shortcut → Properties.
  2. Click Change Icon, browse to an .ico file, and apply the changes.

3. Running the Script with Administrator Privileges

If your script needs admin rights:

  1. Right-click the shortcut → Properties.
  2. Click Advanced → Check Run as administrator.
  3. Click OK and then Apply.

Now, double-clicking the shortcut will launch your Streamlit app with the correct environment activated! 🚀

Example #1: Streamlit Shortcut

cmd /k "F:\PYTHON\STREAMLIT\kinopoisk\.venv\Scripts\activate.bat && python -m streamlit run F:\PYTHON\STREAMLIT\kinopoisk\app.py"

Example #2: PYTHON Shortcut

cmd /k "cd /d F:\PYTHON\BOTS\RollercoinBot-master && .venv\Scripts\activate.bat && python bot5.py"

Posted in Python, StreamlitTags:
Write a comment
© 2025... All Rights Reserved.