Skip to content

Instantly share code, notes, and snippets.

@jonepl
Created August 3, 2022 23:05
Show Gist options
  • Save jonepl/5dc79bcfb8af95f7e323138e1adebe0e to your computer and use it in GitHub Desktop.
Save jonepl/5dc79bcfb8af95f7e323138e1adebe0e to your computer and use it in GitHub Desktop.

Python Virtual Environment

Python venv (virtural environment) comes package with Python 3.

Setting up venv

  1. Execute the following command to create a virtual environment

    Mac

    # Creates venv folder in the current directory
    $ python -m venv venv

    Windows

    REM Creates venv folder in the current directory
    > py -3 -m venv venv

Running venv

  1. Startup your virtual environment:

    Mac

    # Makes venv the default Python interpreter
    $ . venv/bin/activate
    
    # OR
    
    $ source venv/bin/activate 

    Windows

    REM Makes venv the default Python interpreter
    $ .\venv\Scripts\activate.bat
  2. Shutting down virtual environment

    Mac

    # Turns of venv
    $ deactivate

    Windows

    REM Turns of venv
    $ .\venv\Scripts\deactivate.bat

Running Flask application

  1. Install python dependencies

    While venv is activated execute the following command

    Mac

    # Install all application libraries
    $ pip install -r requirements.txt

    Windows

    REM Install all application libraries
    > pip install -r requirements.txt
  2. Start flask application

    Mac

    $ venv/bin/flask run --no-debugger
    

    Windows

    your .flaskenv file will have to look like

        FLASK_APP=app.py 
        FLASK_ENV=development
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment