You've imported code that you wrote in a different file into your scripts before. In Python, this is relatively straight-forward if you're working in a folder structure like shown below:
codingnomads
├── ingredients.py
└── soup.py
If you're working in soup.py, you can get access to anything you've defined inside of ingredients.py with a familiar import statement:
# soup.py
from ingredients import carrot
Import a Module Into Python
This is generally the recommended way of importing from a module, and in this case, it gives you access to the carrot variable that you defined in your ingredients.py file.
Info: You can create the folder structure and files to work alongside this lesson. To make the import work in soup.py, you'll need to define a variable called carrot in ingredients.py.
After you've imported the code, you can now use it in your other script:
# soup.py
from ingredients import carrot
print(carrot)
You can use the carrot variable in any way you want to, just as if you had declared it directly in the soup.py file.
Namespace Preservation
However, there are also other ways of importing modules in Python. Sometimes, it might be helpful to keep it completely clear where some code that you're using is defined. You can shorten your import statement and preserve its namespace:
# soup.py
import ingredients
print(ingredients.carrot)
With this type of import, you need to call any variables or functions defined in ingredients.py through the ingredients. namespace.
Alias Imports
Sometimes, you want to preserve the namespace, but you don't want to keep typing out a long word such as ingredients over and over again. You can assign an alias to your code module during the import:
# soup.py
import ingredients as i
print(i.carrot)
If you assign an alias to your module name during import using the as keyword, then you can access its namespace through the alias you gave it. In the code snippet above, the alias for ingredients is just the single letter i.
When you work with some popular external Python packages, you'll notice that some of them are by default aliased during import, for example:
import pandas as pdimport numpy as np
These aliases are not only conventions that help preserve the namespace of the packages but also allow the developers to type less code. If you haven't encountered these packages during your online research, look them up so you have an idea of what they are used for.
But how can you import your own code from files that aren't right in the same folder as your current script?
Summary: What is Python Import
- For code files that share a folder with each other, you can import your own custom code in the same way that you can import modules from the standard library or even external packages.
- During import, you can decide whether you want to retain the namespace and import everything or pick only specific pieces of information that were defined in the original module.
- You can assign an alias to your imports to reference them in a more convenient way.