Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:20:10+00:00 2026-06-12T18:20:10+00:00

I wrote little wrapper for urllib (python3). Is it proper and safe to import

  • 0

I wrote little wrapper for urllib (python3). Is it proper and safe to import module in if?

if self.response_encoding == 'gzip':
    import gzip

I didn’t find any PEP about this code. However, it bothers me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:20:12+00:00Added an answer on June 12, 2026 at 6:20 pm

    The Python standard library uses it, so it is most definitely proper and safe. See the os module source for an excellent example:

    if 'posix' in _names:
        name = 'posix'
        linesep = '\n'
        from posix import *
        try:
            from posix import _exit
        except ImportError:
          pass
        import posixpath as path
        import posix
        __all__.extend(_get_exports_list(posix))
        del posix
    

    It’s quite common to conditionally import modules in python. Instead of if, you’ll often see a try:/except ImportError: combination too:

    try:
        from subprocess import check_output
    except ImportError:
        # Python 2.6 and before
        def check_output(*popenargs, **kwargs):
            from subprocess import Popen
            if 'stdout' in kwargs:
                raise ValueError('stdout argument not allowed, it will be '
                                 'overridden.')
            process = Popen(stdout=PIPE, *popenargs, **kwargs)
            output, unused_err = process.communicate()
            retcode = process.poll()
            if retcode:
                cmd = kwargs.get("args")
                if cmd is None:
                    cmd = popenargs[0]
                raise CalledProcessError(retcode, cmd)
            return output
    

    Here, we basically use the moral equivalent of an if test: If you can import check_output, do so, otherwise define the full function here.

    An import statement is just a re-binding of an external piece of code to a local name. Using an if control flow to control the import is no different from assigning a variable in an if statement in that regard. You need to make sure you don’t end up using the name without it being defined either way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a little Haskell program to find the area of a triangle, primarily
I wrote little wrapper subroutine to produce HTML-tables with HTML::Table , same time i
I wrote this tiny little wrapper around order , but I fear my implementation
I wrote a little wrapper using beautifulsoup great html parser recently I tried to
I wrote a little C++ wrapper around some parts of GSL and encounter the
I wrote little wrapper for XML Lite library to use it in my MFC
I wrote little WPF application with 2 threads - main thread is GUI thread
I wrote a little piece of code to expose my problem. public class date
I wrote a little program that use activemq embedded broker. Program run on one
i wrote a little addin, which does some formatting of my C# code. in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.