Question Details

No question body available.

Tags

python import

Answers (7)

November 2, 2025 Score: 8 Rep: 975 Quality: Low Completeness: 20%

PEP 8 explicitly recommends placing imports at the top of the file.

PEP8

November 2, 2025 Score: 7 Rep: 29 Quality: Medium Completeness: 40%

A couple issues off the top of my head:

  1. When you call the function if the library is large it might slow down your algorithm. Whether it matters or not depends on the context and end user. This drawback could be a gain if the goal is to reduce initial script loading time.

  2. If the library isn't installed your code may only fail when the function is called, which could cause a delay in failure. It is often better to fail as soon as the script is loaded so you immediately know there is a problem.

  3. It's easier to read and debug code that adheres to formatting standards.

  4. Potential linter implications.

In the end the drawbacks depend on the context entirely. I think the more important thing to consider is what you can accomplish by doing this, which often is very little.

November 2, 2025 Score: 5 Rep: 10,031 Quality: Medium Completeness: 80%

It is not a good practice to import a library inside a function (if it is not a special case where the function logic is done for it), because it may affect on the duration, some libraries may do extra calculation on import and so on. Also remember the point in The Zen of Python: "Explicit is better than implicit.". PEP8 recomments keeping imports on the top as well.

If you develop your own library it is recommended to list the necessary dependencies carefully. So normally it is supposed that if you install a library it installs all the related libraries automatically. But likely in your case the dependency may be too heavy, so instead of installing it as usually, you want to have an opportunity to use the library limitedly for some simple cases. So a good practice could be a conditional import like this and checking the availability on the function call.

import warnings
try:
    import numpy
except ImportError:
    warnings.warn("numpy is not installed, some functions may not work correctly")

...

def func3(): assert 'numpy' in globals(), "This function can't be run unless you install numpy" ...

Here you should not specify numpy in the dependencies, but if you access the module you will see the warning, and if you call func3 you will get a clear error.

November 2, 2025 Score: 4 Rep: 187 Quality: Low Completeness: 0%

@Kevin But it might slow down the function only on the first call, since each module is only imported once per interpreter session.

November 2, 2025 Score: 3 Rep: 34,146 Quality: Medium Completeness: 70%

Generally, no, because you want to fail fast, i.e. if the module can't be imported, error as soon as possible, rather than erroring only when the function is first run.

For more details, see Software Engineering SE: Why should imports be made at the beginning

In your case, you might consider refactoring to put func3 in a separate module so that users don't need to import numpy in order use func1 and func2.

November 2, 2025 Score: 3 Rep: 1,191 Quality: Low Completeness: 0%

Personally, I believe that the only scenario where this would be acceptable is if there is a high probability that a certain function will not be used by the user.

November 3, 2025 Score: 0 Rep: 13,604 Quality: Low Completeness: 70%

While I agree with the general consensus and particularly so in the context of numpy, I want to highlight a counter example from Flask where one typically imports blueprints inside the app factory:

per: Flask Application Factories

def createapp(configfilename):
    app = Flask(name)
    app.config.frompyfile(configfilename)

from yourapplication.model import db db.initapp(app)

from yourapplication.views.admin import admin from yourapplication.views.frontend import frontend app.register
blueprint(admin) app.register_blueprint(frontend)

return app