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 90887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:56:37+00:00 2026-05-10T22:56:37+00:00

How do you gracefully handle failed future feature imports? If a user is running

  • 0

How do you gracefully handle failed future feature imports? If a user is running using Python 2.5 and the first statement in my module is:

from __future__ import print_function 

Compiling this module for Python 2.5 will fail with a:

  File '__init__.py', line 1     from __future__ import print_function SyntaxError: future feature print_function is not defined 

I’d like to inform the user that they need to rerun the program with Python >= 2.6 and maybe provide some instructions on how to do so. However, to quote PEP 236:

The only lines that can appear before a future_statement are:

  • The module docstring (if any).
  • Comments.
  • Blank lines.
  • Other future_statements.

So I can’t do something like:

import __future__  if hasattr(__future__, 'print_function'):     from __future__ import print_function else:     raise ImportError('Python >= 2.6 is required') 

Because it yields:

  File '__init__.py', line 4     from __future__ import print_function SyntaxError: from __future__ imports must occur at the beginning of the file 

This snippet from the PEP seems to give hope of doing it inline:

Q: I want to wrap future_statements in try/except blocks, so I can use different code depending on which version of Python I’m running. Why can’t I?

A: Sorry! try/except is a runtime feature; future_statements are primarily compile-time gimmicks, and your try/except happens long after the compiler is done. That is, by the time you do try/except, the semantics in effect for the module are already a done deal. Since the try/except wouldn’t accomplish what it looks like it should accomplish, it’s simply not allowed. We also want to keep these special statements very easy to find and to recognize.

Note that you can import __future__ directly, and use the information in it, along with sys.version_info, to figure out where the release you’re running under stands in relation to a given feature’s status.

Ideas?

  • 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. 2026-05-10T22:56:38+00:00Added an answer on May 10, 2026 at 10:56 pm

    ‘I’d like to inform the user that they need to rerun the program with Python >= 2.6 and maybe provide some instructions on how to do so.’

    Isn’t that what a README file is for?

    Here’s your alternative. A ‘wrapper’: a little blob of Python that checks the environment before running your target aop.

    File: appwrapper.py

    import sys major, minor, micro, releaselevel, serial = sys.version_info if (major,minor) <= (2,5):     # provide advice on getting version 2.6 or higher.     sys.exit(2) import app app.main() 

    What ‘direct import’ means. You can examine the contents of __future__. You’re still bound by the fact the a from __future__ import print_function is information to the compiler, but you can poke around before importing the module that does the real work.

    import __future__, sys if hasattr(__future__, 'print_function'):      # Could also check sys.version_info >= __future__. print_function.optional     import app     app.main() else:     print 'instructions for upgrading' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The only clipping that changes the visible rect is from… May 11, 2026 at 2:36 pm
  • added an answer Provided that both your queries always return exactly one row,… May 11, 2026 at 2:36 pm
  • added an answer Be aware that showModalDialog is IE specific and won't necessarily… May 11, 2026 at 2:36 pm

Related Questions

We've been having a problem with a mass mailer component that I wrote for
I have a site that I've developed that makes decent use of Javascript, and
When using Aspect Oriented Programming with Policy injection, how do you deal with policy
Which is the best timer approach for a C# console batch application that has

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.