Python Virtual Environment

Tested on Synology 216

A Python virtual environment is a copy of an existing version of Python with the option to inherit existing packages. 

Update pip

sudo pip3 install --upgrade pip

Install the virtualenv package

The virtualenv package is required to create virtual environments. Install it with pip

sudo pip install virtualenv

Choose a location for the virtual environment

cd /volume1/homes/Daniel

Create the virtual environment

sudo virtualenv virtual_python_test

Activate & deactivate the virtual environment

source virtual_python_test/bin/activate

and to deactivate simply

deactivate

Installing modules

Example (when inside the activated environment)

sudo pip3 install flask

Example

Place below code inside the above created virtual_python folder

from flask import Flask  # might need to sudo pip3 install flask
from datetime import datetime

app = Flask(__name__)

@app.route('/')
def hello():
	now = datetime.now()
	current_time = now.strftime("%H:%M:%S")
	return "current time is:" + current_time

if __name__ == '__main__':
    app.run(debug=False, host="192.168.0.216", port=18085)

Start it with

python3 flask_test.py 

Test it http://192.168.0.216:18085

Leave a Reply

Your email address will not be published. Required fields are marked *