{"id":737,"date":"2013-03-01T00:00:00","date_gmt":"2013-03-01T00:00:00","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2013\/03\/01\/how-do-you-import-a-python-module-from-another-folder\/"},"modified":"2021-07-24T18:07:17","modified_gmt":"2021-07-24T18:07:17","slug":"how-do-you-import-a-python-module-from-another-folder","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Programming\/Python\/how-do-you-import-a-python-module-from-another-folder.html","title":{"rendered":"How do I import a python module from another folder ?"},"content":{"rendered":"
Within this article we will show you how to import a python module that is located within a different folder.<\/p>\n
This example will be based upon your folder structure including a folder named ‘module’ and the path ‘\/opt\/django\/myproject\/’ already being within your python system path.<\/p>\n
Below shows you a quick example of the layout and the location of the module that we will later import.<\/p>\n
\/opt\/django\/myproject
.
|– __init__.py
|– modules
| |– __init__.py
| |– validate.py<\/p>\n
Next check your python system paths. <\/p>\n
Python 2.4.3 (#1, Jan 9 2013, 06:47:03) Ensure that a file called __init__ is added to the folder that contains the module you want to import. This file is typically empty.<\/p>\n Finally import the module using the following syntax ‘from <folder>.<filename> import <module>’. So based on our example this will look like,<\/p>\n Python 2.4.3 (#1, Jan 9 2013, 06:47:03) <\/p>\n","protected":false},"excerpt":{"rendered":" Within this article we will show you how to import a python module that is located within a different folder. This example will be based upon your folder structure including a folder named ‘module’ and the path ‘\/opt\/django\/myproject\/’ already being within your python system path. Example Folder Structure Below shows you a quick example of … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"yoast_head":"\n
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
(InteractiveConsole)
>>> import sys<\/span><\/strong>
>>> print sys.path<\/span><\/strong>
[‘\/opt\/django\/myproject<\/span>‘, …… (remaining output omitted)<\/p>\nAdd __init__<\/strong><\/h3>\n
Import Module<\/strong><\/h3>\n
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
(InteractiveConsole)
>>> from modules.validate import *<\/span><\/strong><\/p>\n