Can I use my own shorthand for imported modules?

Question

When importing modules in Python, can I use my own shorthand for them, or do I have to follow the ones that Codecademy uses?

Answer

You can absolutely use your own shorthand for imported modules, and are not limited to using only the shorthand used in Codecademy, such as
import numpy as np

In the Codecademy lessons, we stick to specific shorthand for modules, but when you work outside of Codecademy, you can use your own.

There are a few things that might be important to keep in mind when applying shorthand for module imports.

  • They should not conflict with any reserved keywords in Python. So avoid shorthand like import module as False, since False is a reserved keyword.
  • Similar to the above point, you should also avoid using the same shorthand as an existing variable name, which will most likely cause issues.
  • Usually, we use shorthand to shorten the name of a module or function we import. So, you should only really use shorthand if it is shorter than the imported module name, otherwise it would not be too useful, unless you prefer using a different name.
  • Try to use shorthand that is easily identifiable and easy to understand, like np for numpy.
  • You do NOT have to use shorthand if it’s not necessary. However, it does save a lot of time by shortening the module names, and can reduce typos since there is less typing. Depending on who else is working on the program, they may prefer a certain shorthand, or not use any at all, so do what works best in the situation.
3 Likes