A misconception about package import in Python
Python is a powerful language that allows developers to quickly and easily add standard functionality to their programs by using built-in and external packages. These packages can be installed into the Python environment and imported into our code using simple import statements. However, there is a common misconception among developers, both new and experienced, about memory usage during package import in Python. In this article, we’ll dive into this misconception and explore how importing packages affects memory usage in Python. So, if you’re ready to boost your Python skills and learn the truth about package import in Python, let’s get started!
Multiple methods of importing packages
There are two main ways to import and use the packages in Python. Here are some examples:
Method 1: Import the entire package
The first one is importing the entire package and using only the required functionalities. The following example imports the very popular NumPy
package as np and uses only the constant e
to print it into the console.
import numpy as np
print(np.e)
PythonMethod 2: Import specific items from the package
Another way is importing only the required item from the package without importing the entire package. The following code snippet imports only e from the package NumPy
and prints it into the screen.
from numpy import e
print(e)
PythonMisconception about package import
Among the above-mentioned two methods, many developers might assume that the first method(importing the entire package) consumes more memory than the second method(importing only the required items). However, this is a common misconception. In fact, Python uses exactly the same amount of memory whether you import the entire package or only a few items from the package. This is so because Python reads and loads the entire code of the imported package before importing the specified items even in the second method.
This has also been explained by Youtuber Reuven Lerner in his video.
Testing the memory usage
Let’s test if this is the case with a simple code.
Stackoverflow user Basj describes a simple method to view the total memory used by a Python process. The following code snippet uses psutil
to print the memory usage of the Python program in megabytes(MB). I used this code to view the memory usage while testing.
import psutil
process = psutil.Process()
print(process.memory_info().rss / (1024 ** 2))
PythonTest 1: Importing the entire package
The following code snippet imports the entire NumPy
package, prints the value of e using the NumPy
module, and prints the memory usage in MB.
import numpy as np
import psutil
process = psutil.Process()
print(process.memory_info().rss / (1024 ** 2))
print(np.e)
Memory Usage: 31.203125 MBThe above code used 31.2 MB of memory on average.
Note: actual memory usage on your system might be slightly different.
Test 2: Importing a specific item from the package
The following code snippet imports only the item e
from the NumPy package and prints its value, and shows the memory usage in MB.
from numpy import e
import psutil
process = psutil.Process()
print(process.memory_info().rss / (1024 ** 2))
print(e)
Memory Usage: 31.20703125 MBThe above code also used 31.2 MB of memory on average.
Conclusion
After conducting tests, we found that Python consumes the same amount of memory whether we import the entire package or just the required items from the package. Therefore, it is not possible to save memory by importing the specific items in Python. Please note that this might not be the case in other programming languages.
If you have any comments or suggestions regarding this post or any other programming topic, please feel free to post them below.
You can also read more articles on Programming and Python on this site.