简体   繁体   中英

print(“hello world”, end=' ') causes syntax error

I have created a module that I use in another program. The module prints some text, sleeps for a second, and then prints some more text (on the same line, and without any spaces). I am running python 2.7, and from __future__ import print_function is the first line in the program that calls my module, but when I import my module, I get this error:

Traceback (most recent call last):
  File "C:\######\document.py", line 3
1, in <module>
    import mymodule
  File "C:\######\document.py", line 2
    print("hello world", end=' ')
                          ^
SyntaxError: invalid syntax

here's the code:

def run():
    print("hello world", end =' ')
    time.sleep(0.5)
    print(".", end =' ')
    time.sleep(0.5)
    print(".", end =' ')
    time.sleep(0.5)
    print(".")
    time.sleep(0.25)

How can I fix this without importing print_function twice?

I am running python 2.7, and have imported print_function in the program that calls my module

That doesn't work. You have to do the future statement in this module , not in the script or module that imports it.

See the documentation for details. But the short version is: The future statement changes the way your module is compiled, so Python has to be able to see it at module compilation time, not just at runtime. (That's also why it has to be the first non-comment/docstring line in the file.)


OK, so that explains how to fix it, but it doesn't explain why there's a problem unless you already know when module compilation happens.

Oversimplifying a bit: When you import spam (if it hasn't already been imported during this session), Python goes looking for an appropriate spam.py file. If it finds one, it then looks for a spam.pyc that's newer than spam.py . If so, it just executes that. If not, it compiles spam.py into spam.pyc , then executes it.

So, because future statements affect the way code is compiled, if Python wanted to let your future statements affect other modules that you imported, it would have to store a different version of spam.pyc for each possible combination of future statements—maybe spam.pyc , spam+print_function.pyc , spam+division.pyc , spam+print_function+division.pyc , etc.


Finally:

How can I fix this without importing print_function twice?

You really can't. Well, you can , but you don't want to. The cleanest and simplest way I can imagine doing this is to write an import hook that overrides the usual mechanism for looking for and compiling .pyc files to ensure that everything you import is treated as if it had all of your future statements applied (possibly using something like the spam+print_function.pyc trick I semi-facetiously suggested above, to make sure you don't collide with normal cache .pyc files. (See the compile docs for info on letting the modules inherit your future flags, or specifying a set of them explicitly.)

If that sounds really cool to you, learning about Python's import system is a lot of fun, but I would strongly suggest waiting until you upgrade to at least Python 3.4 before doing so, because it's changed a lot, and for the better; it's a lot easier to learn (especially since the whole thing is written in Python and thoroughly documented, instead of a mess of C code scattered around three different places), and a lot more flexible (so you usually don't have to duplicate half of what Python normally does for you, you can just replace the one part you want to change).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM