# Import Modules in Python Reference: [Python Tutorial for Beginners 9: Import Modules and Exploring The Standard Library](https://youtu.be/CqvZ3vGoGs0?t=373) Case 1: Assume we have two scripts which are placed in the same direcotry. --- ``` Decktop │ my_module.py │ intro.py └─ ``` We can import `my_module` because it's in the same directory as our `intro.py` --- ```python ### my_module.py ### print('Imported my_module...') test = 'Test String' def find_index(to_search, target): '''Find the index of a value in a sequence''' for i, value in enumerate(to_search): if value == target: return i return -1 ``` ```python ### intro.py ### import my_module as mm courses = ['History', 'Math', 'Physics', 'CompSci'] index = mm.find_index(courses, 'Math') print(index) # 1 ``` ### Q: Why we can import that directly? ### A: Because the module path is in `python environment path` - Python will look for modules in the `environment path` - p.s. We can check this by print out the `sys.path` ```python ### intro.py ### import sys print(sys.path) import my_module as mm courses = ['History', 'Math', 'Physics', 'CompSci'] index = mm.find_index(courses, 'Math') print(index) # 1 ``` ### output of `sys.path` ``` ['/Users/joe/Desktop', '/Users/joe/miniconda3/lib/python37.zip', '/Users/joe/miniconda3/lib/python3.7', '/Users/joe/miniconda3/lib/python3.7/lib-dynload', '/Users/joe/miniconda3/lib/python3.7/site-packages'] ``` #### So as you can see the 1st element of `sys.path list` is `/Users/joe/Desktop` Case 2: Assume we have two scripts which aren't placed in the same direcotry. --- ``` Decktop │ │ intro.py │ └───my_modules │ └─ my_module.py └─ ``` ## Method 1: Append the path of `my_module.py` before importing `my_module` ```python ### intro.py ### import sys sys.path.append('/Users/joe/Desktop/my_modules') courses = ['History', 'Math', 'Physics', 'CompSci'] from my_module import find_index index = find_index(courses, 'Math') print(index) # 1 ``` ### output of `sys.path` ``` ['/Users/joe/Desktop', '/Users/joe/miniconda3/lib/python37.zip', '/Users/joe/miniconda3/lib/python3.7', '/Users/joe/miniconda3/lib/python3.7/lib-dynload', '/Users/joe/miniconda3/lib/python3.7/site-packages', '/Users/joe/Desktop/my_modules'] # the path that we append. ``` #### So as you can see the last element of `sys.path list` is `/Users/joe/Desktop/my_modules` #### So that's where and why python can find the `my_module.py` ### What is `sys.path`? - This is the `list of directories` my machine where python looks for modules where we run an import - The first value here is just the `directory where I'm currently running the script from` and the `my_module.py` that we're importing is within that directory also, so that's how it found it. ### Q: So what directories are added to this sys.path list? ### A: Directories are added in this order: - First the directory containing the script that we're `currently running`. - And next it adds directories listed in the `Python path environment variable`. - And after the Python path it then adds the `standard library diectories`. - And lastly, it adds the site packages directory for `third-party packages`. ### Drawbacks of adding environment path by `sys.path.append` - This isn't the best looking approach, because we're appending this directory before our other imports. - And also if we were to import our module and we had this maunally hard-coded in multiple locations. - And we had to change all of those. ## Method 2: Change`Python Path Environment variables` - p.s. Changing the environment variables is different on Mac and Windows, we're only going to show how change on Mac or linux. ### Step1: Open your terminal ### Step2: Edit the `.bash_profile` ``` nano ~/.bash_profile ``` ![](https://i.imgur.com/VGTKvSM.png) ### Step3: Add the path and finish~ ``` export PYTHONPATH="/Users/joe/Desktop/my_modules" ``` p.s. no spaces between PYTHONPATH and "=" ### Step4: Restart terminal or type the follow command in the terminal ``` source ./bash_profile ``` ### Step5: Print out the `sys.path` ``` print(sys.path) ``` ``` ['', '/Users/joe/Desktop/my_modules', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages'] ``` And we see the `'/Users/joe/Desktop/my_modules'` is in the `list`! ###### tags: `Python`