{"id":602,"date":"2017-02-25T17:11:39","date_gmt":"2017-02-25T09:11:39","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=602"},"modified":"2018-09-05T00:21:47","modified_gmt":"2018-09-04T16:21:47","slug":"a-platform-independent-way-to-set-your-python-path-for-your-python-applications","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/a-platform-independent-way-to-set-your-python-path-for-your-python-applications\/","title":{"rendered":"A platform independent way to set your Python Path for your Python applications"},"content":{"rendered":"<p>In a software development house where desktop computers run Microsoft Windows while servers run Linux, software developers will have to ensure that the Python code that they wrote on their Windows machine can run on the deployment servers which are running Linux. <\/p>\n<p>One unavoidable task for Python application developers is the importing of functionalities that are contained in other Python scripts. In order for the Python interpreter to find the Python scripts that are referenced by Python import statements, the Python Path will need to contain the URLs of the directories that contain the Python scripts to be imported.<\/p>\n<p>This post documents how I set my Python Path for my Python applications in a platform independent way.  <\/p>\n<h2>The platform dependent way to set your Python Path<\/h2>\n<p>One platform dependent way that you may configure your Python Path is by setting the <code>PYTHONPATH<\/code> environment variable through a script file targeted at the native command-line interpreter that is provided by the Operating System where your Python application will run on.<\/p>\n<p>To run your Python application on Windows, you may create a batch file, <code>run.bat<\/code> with the following contents:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nset PYTHONPATH=%PYTHONPATH%;C:\\your_python_lib\r\n\r\npython app.py\r\n<\/pre>\n<p>And to run your Python application on Linux, you may create a shell script, <code>run.sh<\/code> with the following contents:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nexport PYTHONPATH=$PYTHONPATH:\/your_python_lib\r\n\r\npython app.py\r\n<\/pre>\n<p>This approach of configuring Python Path for your Python applications will result in duplicated effort whenever you import new library scripts that are located in new directories.<\/p>\n<h2>The platform independent way to set your Python Path<\/h2>\n<p>Setting your Python Path in a platform independent way will avoid duplicated effort whenever you need to import new library scripts that are located in new directories. I typically follow the following guidelines to set my Python Path in a platform independent way:<\/p>\n<ul>\n<li>Place the library scripts in a directory that is near to the starter script.<\/li>\n<li>Set Python Path inside the starter script.<\/li>\n<\/ul>\n<h3>Placing the library scripts in a directory that is near to the starter script<\/h3>\n<p>I tend to follow the following directory structure whenever I need to create new Python applications:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n.\r\n|--app.py\r\n|--import\r\n   |--techcoil\r\n      |--__init__.py\r\n      |--controller\r\n         |--__init__.py\r\n         |--users.py\r\n         !--products.py\r\n      |--storage\r\n         |--__init__.py\r\n         |--mongo.py\r\n         |--file.py\r\n      |--service\r\n         |--__init__.py\r\n         |--users.py\r\n         |--products.py\r\n<\/pre>\n<p>In the above directory structure, I designate <code>app.py<\/code> as the starter script. I then designate a directory , which I named as <code>import<\/code>, in the same directory as <code>app.py<\/code>. I will place the module packages that my Python application need to import inside the <code>import<\/code> directory.<\/p>\n<h3>Setting Python Path inside the starter script<\/h3>\n<p>To set the Python Path inside the starter script, I will include the following Python codes at the top:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport sys\r\nroot_directory = os.path.dirname(os.path.realpath(__file__))\r\nsys.path.append(os.path.join(root_directory, 'import'))\r\n\r\n### Application specific logic ###\r\n<\/pre>\n<p>In the above codes, I first import the <a href=\"https:\/\/docs.python.org\/3\/library\/os.html\" target=\"_blank\"><code>os<\/code><\/a> and <a href=\"https:\/\/docs.python.org\/3\/library\/sys.html\" target=\"_blank\"><code>sys<\/code><\/a> in-built Python modules. I then proceed to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-get-the-directory-path-of-a-python-3-script-from-within-itself\/\" target=\"_blank\">get the directory path where the executing script resides in<\/a> (in this case, <code>app.py<\/code>) and set the value to the <code>root_directory<\/code> variable.<\/p>\n<p>I then construct the url to the <code>import<\/code> directory and append it to the <a href=\"https:\/\/docs.python.org\/3\/library\/sys.html#sys.path\" target=\"_blank\"><code>sys.path<\/code><\/a> variable. This will make the modules inside the <code>import<\/code> directory visible to the Python interpreter.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-9I\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-9I&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-9I&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F602&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>In a software development house where desktop computers run Microsoft Windows while servers run Linux, software developers will have to ensure that the Python code that they wrote on their Windows machine can run on the deployment servers which are running Linux. <\/p>\n<p>One unavoidable task for Python application developers is the importing of functionalities that are contained in other Python scripts. In order for the Python interpreter to find the Python scripts that are referenced by Python import statements, the Python Path will need to contain the URLs of the directories that contain the Python scripts to be imported.<\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[383,226,381,255,384,382],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-9I","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/602"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=602"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/602\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}