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. 

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
mkdir virtual_env_test
cd virtual_env_test

Create the virtual environment

sudo virtualenv virtual_python

Activate & deactivate the virtual environment

source virtual_python/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.