{"id":127,"date":"2024-01-25T11:28:12","date_gmt":"2024-01-25T11:28:12","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=127"},"modified":"2024-01-25T11:28:13","modified_gmt":"2024-01-25T11:28:13","slug":"python-module-search-path","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-module-search-path\/","title":{"rendered":"Python Module Search Path"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how the module search path works in Python when you import a module into a program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python module search path<\/h2>\n\n\n\n<p>When you import a\u00a0module\u00a0in a program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import module<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Python will search for the&nbsp;<code>module.py<\/code>&nbsp;file from the following sources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The current folder from which the program executes.<\/li>\n\n\n\n<li>A list of folders specified in the\u00a0PYTHONPATH\u00a0environment variable, if you set it before.<\/li>\n\n\n\n<li>An installation-dependent list of folders that you configured when you installed Python.<\/li>\n<\/ul>\n\n\n\n<p>Python stores the resulting search path in the&nbsp;<code>sys.path<\/code>&nbsp;variable that comes from the&nbsp;<code>sys<\/code>&nbsp;module.<\/p>\n\n\n\n<p>The following program shows the current module search path:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import sys for path in sys.path: print(path) <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Here\u2019s a sample output on Windows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>D:\\Python\\ C:\\Program Files\\Python38\\python38.zip C:\\Program Files\\Python38\\DLLs C:\\Program Files\\Python38\\lib C:\\Program Files\\Python38 C:\\Users\\PythonTutorial\\AppData\\Roaming\\Python\\Python38\\site-packages C:\\Program Files\\Python38\\lib\\site-packages <\/code><small>Code language: Shell Session (shell)<\/small><\/code><\/pre>\n\n\n\n<p>And the following is the sample output on Linux:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>\/Library\/Frameworks\/Python.framework\/Versions\/3.8\/bin \/Library\/Frameworks\/Python.framework\/Versions\/3.8\/lib\/python38.zip \/Library\/Frameworks\/Python.framework\/Versions\/3.8\/lib\/python3.8 \/Library\/Frameworks\/Python.framework\/Versions\/3.8\/lib\/python3.8\/lib-dynload \/Library\/Frameworks\/Python.framework\/Versions\/3.7\/lib\/python3.8\/site-packages<\/code><small>Code language: Shell Session (shell)<\/small><\/code><\/pre>\n\n\n\n<p>To make sure Python can always find the&nbsp;<code>module.py<\/code>, you need to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Place&nbsp;<code>module.py<\/code>&nbsp;in the folder where the program will execute.<\/li>\n\n\n\n<li>Include the folder that contains the&nbsp;<code>module.py<\/code>&nbsp;in the&nbsp;<code>PYTHONPATH<\/code>&nbsp;environment variable. Or you can place the&nbsp;<code>module.py<\/code>&nbsp;in one of the folders included in the&nbsp;<code>PYTHONPATH<\/code>&nbsp;variable.<\/li>\n\n\n\n<li>Place the&nbsp;<code>module.py<\/code>&nbsp;in one of the installation-dependent folders.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Modifying the Python module search path at runtime<\/h2>\n\n\n\n<p>Python allows you to modify the module search path at runtime by modifying the&nbsp;<code>sys.path<\/code>&nbsp;variable. This allows you to store module files in any folder of your choice.<\/p>\n\n\n\n<p>Since the&nbsp;<code>sys.path<\/code>&nbsp;is a list, you can append a search-path to it.<\/p>\n\n\n\n<p>The following example adds the\u00a0<code>d:\\modules<\/code>\u00a0to the search path and use the\u00a0<code>recruitment<\/code>\u00a0module stored in this folder:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>>>> import sys >>> sys.path.append('d:\\\\modules\\\\') >>> import recruitment >>> recruitment.hire() Hire a new employee...<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how the module search path works in Python when you import a module into a program. Introduction to Python module search path When you import a\u00a0module\u00a0in a program: Python will search for the&nbsp;module.py&nbsp;file from the following sources: Python stores the resulting search path in the&nbsp;sys.path&nbsp;variable that comes from the&nbsp;sys&nbsp;module. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-9-modules-packages"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":128,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/127\/revisions\/128"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}