A shell script that activates a Python virtual environment and then runs a specific Python script

CK CK 3788 
Created at  
741 0 0

Below is a shell script that activates a Python virtual environment and then runs a specific Python script.

Assuming:

  • The virtual environment is located in a directory named venv.
  • The Python script you want to run is named script.py.

Here is the shell script:

#!/bin/bash

# Set the virtual environment directory and the Python script
VENV_DIR="venv"
SCRIPT_NAME="script.py"

# Activate the virtual environment
source "$VENV_DIR/bin/activate"

# Run the Python script
python "$SCRIPT_NAME"

# Deactivate the virtual environment
deactivate

Steps to Use the Script:

  1. Save this shell script to a file, e.g., run_script.sh.
  2. Give the script execute permissions:

    chmod +x run_script.sh
    
  3. Run the script:

    ./run_script.sh
    

This script activates the virtual environment, runs the specified Python script, and then deactivates the virtual environment. Adjust the VENV_DIR and SCRIPT_NAME variables as needed for your specific setup.

Tags Pyhton Shell Script Python Virtual Environment Shell Script Facebook X
Comments 0
Similar posts
  1. Building Initial Virtual Environment at Python - The first step you should do for Python Programming
    559
  2. Run Python Code with Virtual Environment in PHP
    671
  3. How to get the duration between days in PHP
    419
  1. Python code using GPU on CentOS with AWS
    1,247
  2. Video Scence Detection with PySceneDetect
    2,318
  3. Python Audio Separation Libraries allowing you to isolate individual tracks
    1,433
  4. Python Version Control for Library Compatibility with pyenv - Downgrading/Upgrading Python Version
    902
  5. Deleting a specific user voice from the original audio track with Python
    547
  6. Creating an MR from the original MP3 audio in Python - Deleting Voice Track from Audio File
    436
  7. Creating mp3 audio file based on the text input with Python
    463
  8. Implementing Text To Speech / Audio with Python3 and pyttsx3
    354
  9. Building Initial Virtual Environment at Python - The first step you should do for Python Programming
    559
  10. Python Simplified Assignment Operators
    533
  11. Trackback module used for Python Threading debugging
    840
  12. Python Flask with simple HTML files to server web pages along with API
    926
  13. Python Flask web server tutorial from the scratch
    618
  14. Simple Web Server based in Python
    368
  15. Thread Implementation in Python
    507
  16. Learning about Python Multiprocessing Technique
    590
  17. Simple Example to get the total number of records in BigQuery with Python
    665
  18. Using BigQuery in Python
    407
  19. Python Web Programming based on Django from the Scratch
    366
  20. Flink with Python: Core Concepts, Kafka Integration, and Architectural Trade-offs
    184
  21. How do I determine the client IP type in Python - IPv4 or IPv6
    759
  22. Predicting Buyer Preferences with PyTorch: A Deep Learning Approach
    1,230
  23. PyTorch example to Forcast Stock Price based on 10 days Dataset
    651
  24. Implementing a Versatile DNS Server in Python: Handling A, AAAA, CNAME, and TXT Records
    1,067
  25. Dynamic DNS Made Easy: Building a Python-Based Solution
    793
  26. Milvus & FAISS: Decoding Their ML Synergy
    276
  27. Save clustering result (aka dictionary) in JSON and Reuse loaded result
    376
  28. Forecasting with Linear Regression and KNN Regression in Python
    920
  29. Forecasting the Weather with PyTorch: A Beginner's Guide to Temperature Prediction
    1,430
  30. Uvicorn, Gunicorn: Proving Your Production Prowess
    395
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,703
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    197
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    508
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    575
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    252
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    326
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252