{"id":105,"date":"2022-04-23T15:08:26","date_gmt":"2022-04-23T15:08:26","guid":{"rendered":"https:\/\/hb9iiu.com\/?p=105"},"modified":"2022-04-23T15:08:26","modified_gmt":"2022-04-23T15:08:26","slug":"python-virtual-environments","status":"publish","type":"post","link":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/2022\/04\/23\/python-virtual-environments\/","title":{"rendered":"Python Virtual Environment"},"content":{"rendered":"\n<p><em><sub>Tested on Synology 216<\/sub><\/em><\/p>\n\n\n\n<p>A Python virtual environment is a copy of an existing version of Python with the option to inherit existing packages.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"install-the-virtualenv-package\">Update pip<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo pip3 install --upgrade pip<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"install-the-virtualenv-package\">Install the virtualenv package<\/h4>\n\n\n\n<p>The virtualenv package is required to create virtual environments. Install it with pip<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo pip install virtualenv<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Choose a location for the virtual environment<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">cd \/volume1\/homes\/Daniel<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create the virtual environment<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo virtualenv virtual_python_test<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Activate &amp; deactivate the virtual environment<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">source virtual_python_test\/bin\/activate<\/pre>\n\n\n\n<p>and to deactivate simply<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">deactivate\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Installing modules<\/h4>\n\n\n\n<p>Example (when inside the activated environment)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo pip3 install flask\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Place below code inside the above created virtual_python folder<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask  # might need to sudo pip3 install flask\nfrom datetime import datetime\n\napp = Flask(__name__)\n\n@app.route('\/')\ndef hello():\n\tnow = datetime.now()\n\tcurrent_time = now.strftime(\"%H:%M:%S\")\n\treturn \"current time is:\" + current_time\n\nif __name__ == '__main__':\n    app.run(debug=False, host=\"192.168.0.216\", port=18085)\n<\/pre>\n\n\n\n<p>Start it with<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 flask_test.py <\/pre>\n\n\n\n<p>Test it <a href=\"http:\/\/192.168.0.216:18085\">http:\/\/192.168.0.216:<\/a><a href=\"http:\/\/192.168.0.216:18085\" target=\"_blank\" rel=\"noreferrer noopener\">18085<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tested on Synology 216 A Python virtual environment is a copy of an existing version of Python with the option to inherit existing packages.&nbsp; Update pip sudo pip3 install &#8211;upgrade pip Install the virtualenv package The virtualenv package is required&#8230; <a href=\"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/2022\/04\/23\/python-virtual-environments\/\" class=\"readmore\">Read more<span class=\"screen-reader-text\">Python Virtual Environment<\/span><span class=\"fa fa-angle-double-right\" aria-hidden=\"true\"><\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-linux","content-layout-excerpt-thumb"],"_links":{"self":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":0,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/105\/revisions"}],"wp:attachment":[{"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hb9iiu.synology.me\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}