简体   繁体   中英

SyntaxError when running with Python 2

I have a Python script called "controlled_biomass_exp.py" that generates some data and plots it. Its over 100 lines long so I don't want to dump it all here.

I can run it from Ipython in the terminal once and it works fine. If I repeat the command to run the script again with:

In [3]: run controlled_biomass_exp.py

I get:

File "< ipython-input-3-3ec3d096e779>", line 1

run controlled_biomass_exp.py

  ^ 

SyntaxError: invalid syntax

(The carrot is pointing at the last letter of the filename, "p".)

I get the same problem if I run any other python script after running this one. If I quit Ipython in the terminal and restart it the problem "re-sets". I can run other scripts fine, until I run the broken one once. I haven't encountered a problem like this before. Any help directing me where to look for solutions much appreciated.

It appears that your script controlled_biomass_exp.py overwrites run in your current namespace.

This toy example will produce a similar problem:

# file: test.py
run = "hello world!"
print(run)

Calling run in IPython is just a shortcut for %run which is a built-in magic function. Once you overwrite run (eg as shown in my toy example) you cannot use the shortcut anymore.

However, %run controlled_biomass_exp.py should still work for you.

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