Does python import all the listed libraries?

I'm just wondering, I often have really long python files and imports tend to stack quite quickly.

PEP8 says that the imports should always be written at the beginning of the file.


Do all the imported libraries get imported when calling a function coded in the file? Or do only the necessary libraries get called?

Does it make sense to worry about this? Is there no reason to import libraries within the functions or classes that need them?


Asked by: Roland832 | Posted: 27-01-2022






Answer 1

Every time Python hits an import statement, it checks to see if that module has already been imported, and if not, imports it. So the imports at the top of your file will happen as soon as your file is run or imported by another module.

There is some overhead to this, so it's generally best to keep imports at the top of your file so that cost gets taken care of up front.

Answered by: Dainton161 | Posted: 28-02-2022



Answer 2

The best place for imports is at the top of your file. That documents the dependencies in one place and makes errors from their absence appear earlier. The import itself actually occurs at the time of the import statement, but this seldom matters much.

It is not typical that you have anything to gain by not importing a library until you are in a function or method that needs it. (There is never anything to gain by doing so inside the body of a class.) It is rare that you want optional dependencies and even rarer that this is the right technique to get them, though. Perhaps you can share a compelling use case?

Answered by: Briony765 | Posted: 28-02-2022



Answer 3

Does it make sense to worry about this?

No

There no reason to import libraries within the functions or classes that need them. It's just slow because the import statement has to check to see if it's been imported once, and realize that it has been imported.

If you put this in a function that's called frequently, you can waste some time with all the import checking.

Answered by: Alford932 | Posted: 28-02-2022



Answer 4

Imports happen when the module that contains the imports gets executed or imported, not when the functions are called.

Ordinarily, I wouldn't worry about it. If you are encountering slowdowns, you might profile to see if your problem is related to this. If it is, you can check to see if your module can divided up into smaller modules.

But if all the files are getting used by the same program, you'll just end up importing everything anyway.

Answered by: Arthur548 | Posted: 28-02-2022



Answer 5

If a function inside a module is the only one to import a given other module (say you have a function sending tweets, only if some configuration option is on), then it makes sense to import that specific module in the function.

Unless I see some profiling data proving otherwise, my guess is that the overhead of an import statement in a function is completely negligible.

Answered by: Lily751 | Posted: 28-02-2022



Similar questions

python - Can not import osgeo and GDAL libraries

I need the osgeo and GDAL libraries in python code. So, I imported them and I compiled to check whether they are installed or not (I'm not a python expert), and as expected the libraries do not exist. So, I tried to install it using the pycharm setting tools. For the osgeo, I found two possibilities. Django-osgeo-importer. Django-osgeo-importer-client. and the ...


python - Import libraries in lambda layers

I wanted to import jsonschema library in my AWS Lambda in order to perform request validation. Instead of bundling the dependency with my app , I am looking to do this via Lambda Layers. I zipped all the dependencies under venv/lib/python3.6/site-packages/. I uploaded this as a lambda layer and added it to my aws lambda using publish-layer-version and aws lambda update-function-...


python - Where is the right place to import libraries in an OOP code

This question already has answers here:


python - ROS can't import third party libraries

I am trying to use tkinter in my ROS environment but I am having some trouble getting it set up. I added it to my packages.xml file like this <build_depend>roscpp</build_depend> <build_depend>rospy</build_depend> <build_depend>std_msgs</build_depend> <depend>python-tk</depend> <build_export_depend>roscpp</build_export_depend> <build...


python - Can't Import Libraries in Visual Studio Code

I'm using Python on VS Code and I need to use the library python-Levenshtein. I've already installed it with the pip3 install command, but somehow this library won't appear in the drop down after typing import (nor I am able to use it). If I check the pip list it is listed. I selected the interpreter on Python 3.8.5 64-bit; I'm using Ubuntu 20.04 and have everything (VSC,...


python - How to import two libraries with the same name

In Python there are two libraries that can be found on Pypi 'python-magic' and 'filemagic': https://pypi.org/project/python-magic/ https://pypi.org/project/filemagic/ As can be seen from the documents, to import either one, the import ...


How to find all built in libraries in Python

I've recently started with Python, and am enjoying the "batteries included" design. I'e already found out I can import time, math, re, urllib, but don't know how to know that something is builtin rather than writing it from scratch. What's included, and where can I get other good quality libraries from?


OCSP libraries for python / java / c?

Going back to my previous question on OCSP, does anybody know of "reliable" OCSP libraries for Python, Java and C? I need "client" OCSP functionality, as I'll be checking the status of Certs against an OCSP responder, so responder functionality is not that important. Thanks


how do i use python libraries in C++?

I want to use the nltk libraries in c++. Is there a glue language/mechanism I can use to do this? Reason: I havent done any serious programming in c++ for a while and want to revise NLP concepts at the same time. Thanks


How can I use Perl libraries from Python?

I have written a bunch of Perl libraries (actually Perl classes) and I want to use some of them in my Python application. Is there a natural way to do this without using SWIG or writing Perl API for Python. I am asking for a similar way of PHP's Perl interface. If there is no such kind of work for Perl in Python. What is the easiest way to use Perl cl...


macos - Mac Based Python GUI Libraries

I am currently building a GUI based Python application on my mac and was wondering could anyone suggest a good GUI library to use? I was looking at python's gui programming faq and there was a lot of options making it hard to choose. I am developing on snow leopard and cross-platform is not essential (if it makes a differenc...


Tree libraries in python

Closed. This question does not meet Stack Overflow guid...


Are there any libraries to allow Python or Ruby to get info from SVN?

I'm looking for plugins that will allow my codebase to interact with, browse, and poll an SVN server for information about a repository. Trac can do this, but I was hoping there was an easy-to-use library available to accomplish the task, rather than trolling through the Trac codebase. Googling for this returns mostly vague results about storing you...


python - ctypes for static libraries?

I'm attempting to write a Python wrapper for poker-eval, a c static library. All the documentation I can find on ctypes indicates that it works on shared/dynamic libraries. Is there a ctypes for static libraries? I know about cython, but should I use that or recompile the poker-eval into a dynamic library so that I can use ctypes? Thanks, Mike


Ruby Vs Python - Socket Libraries

I have a concept I'd like to work on that requires the use of low-level sockets (i.e.: no frameworks or wrappers, just the standard send/recv pattern included in most standard libraries. I'm familiar with both Ruby and Python, and from my (limited) experience they seem to have similar socket libraries. What I'd like to know is if either language has any advantage, whether it be with performance, stability, ease-of...


python - Good sound libraries?


web crawler - What are the best prebuilt libraries for doing Web Crawling in Python

This question already has answers here:


How to find all built in libraries in Python

I've recently started with Python, and am enjoying the "batteries included" design. I'e already found out I can import time, math, re, urllib, but don't know how to know that something is builtin rather than writing it from scratch. What's included, and where can I get other good quality libraries from?


OCSP libraries for python / java / c?

Going back to my previous question on OCSP, does anybody know of "reliable" OCSP libraries for Python, Java and C? I need "client" OCSP functionality, as I'll be checking the status of Certs against an OCSP responder, so responder functionality is not that important. Thanks


how do i use python libraries in C++?

I want to use the nltk libraries in c++. Is there a glue language/mechanism I can use to do this? Reason: I havent done any serious programming in c++ for a while and want to revise NLP concepts at the same time. Thanks


How can I use Perl libraries from Python?

I have written a bunch of Perl libraries (actually Perl classes) and I want to use some of them in my Python application. Is there a natural way to do this without using SWIG or writing Perl API for Python. I am asking for a similar way of PHP's Perl interface. If there is no such kind of work for Perl in Python. What is the easiest way to use Perl cl...


Python vs. C# Twitter API libraries

Closed. This question does not meet Stack Overflow guid...


d - Calling gdc/dmd shared libraries from Python using ctypes

I've been playing around with the rather excellent ctypes library in Python recently. What i was wondering is, is it possible to create shared D libraries and call them in the same way. I'm assuming i would compile the .so files using the -fPIC with dmd or gdc and call them the same way using the ctypes library. Has anyone tried this ? ...


plot - Python plotting libraries

Closed. This question does not meet Stack Overflow guid...


HTML Agility Pack or HTML Screen Scraping libraries for Java, Ruby, Python?

I found the HTML Agility Pack useful and easy to use for screen scraping web sites. What's the equivalent library for HTML screen scraping in Java, Ruby, Python?


c - Building a Python shared object binding with cmake, which depends upon external libraries

We have a c file called dbookpy.c, which will provide a Python binding some C functions. Next we decided to build a proper .so with cmake, but it seems we are doing something wrong with regards to linking the external library 'libdbook' in the binding: The CMakeLists.txt is as follows: PROJECT(dbookpy) FIND_PACKAGE(PythonInterp) FIND_PACKAGE(PythonLibs) INCLUDE_DIRECTORIES(${PYTHON_INCLUDE...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top