execfile

How can I avoid: “ZipFile instance has no attribute '__exit__''” when extracting a zip file?

梦想的初衷 提交于 2021-02-07 14:47:50
问题 The code is: import sys execfile('test.py') In test.py I have: import zipfile with zipfile.ZipFile('test.jar', 'r') as z: z.extractall("C:\testfolder") This code produces: AttributeError ( ZipFile instance has no attribute '__exit__' ) # edited The code from "test.py" works when run from python idle. I am running python v2.7.10 回答1: I made my code on python 2.7 but when I put it on my server which use 2.6 I have this error : AttributeError: ZipFile instance has no attribute '__exit__' For

How can I avoid: “ZipFile instance has no attribute '__exit__''” when extracting a zip file?

二次信任 提交于 2021-02-07 14:45:52
问题 The code is: import sys execfile('test.py') In test.py I have: import zipfile with zipfile.ZipFile('test.jar', 'r') as z: z.extractall("C:\testfolder") This code produces: AttributeError ( ZipFile instance has no attribute '__exit__' ) # edited The code from "test.py" works when run from python idle. I am running python v2.7.10 回答1: I made my code on python 2.7 but when I put it on my server which use 2.6 I have this error : AttributeError: ZipFile instance has no attribute '__exit__' For

Passing arguments to execfile in python 2.7

这一生的挚爱 提交于 2020-01-07 04:43:07
问题 I need to call one python script from another script,I'm trying to do it with the help of execfile function.I need to pass a dictionary as an argument to the calling function.Is there any possibility to do that? import subprocess from subprocess import Popen -------To read the data from xls----- ret_lst = T_read("LDW_App05") for each in ret_lst: lst.append(each.replace(' ','-')) lst.append(' ') result = Popen(['python','LDW_App05.py'] + lst ,stdin = subprocess.PIPE,stdout = subprocess.PIPE)

how to show async data in UI

╄→гoц情女王★ 提交于 2020-01-06 05:30:08
问题 I wrote an async function that needs to call a program in the server and that program generate a file which needs to load in UI for displaying. I am not sure how to show the result in my UI since execFile is async function and it may take few second results to be ready? Do I need to have kind of infinity loop to check the result is ready in the server? I am using nodejs-express handlebars. router.post('/',function(req, res, next) { const child = execFile('program.exe', ['in.sql'], (error,

Stop execution of a script called with execfile

[亡魂溺海] 提交于 2020-01-01 04:13:06
问题 Is it possible to break the execution of a Python script called with the execfile function without using an if/else statement? I've tried exit() , but it doesn't allow main.py to finish. # main.py print "Main starting" execfile("script.py") print "This should print" # script.py print "Script starting" a = False if a == False: # Sanity checks. Script should break here # <insert magic command> # I'd prefer not to put an "else" here and have to indent the rest of the code print "this should not

Difference between import and execfile

橙三吉。 提交于 2019-12-23 09:45:02
问题 I have a file utils.py containing a function called f1() . From another Python script I can import utils or execfile('utils.py') and have access to f1() . What are the differences between the two methods? 回答1: There are many differences, but from your point of view the most significant is probably that import gives you more control over the namespace in which the objects defined in utils.py end up. Let's consider three variants on import . The first is the one you asked about: import utils

Python: Access from within a lambda a name that's in the scope but not in the namespace

那年仲夏 提交于 2019-12-13 05:43:16
问题 Consider the following code: aDict = {} execfile('file.py',globals(),aDict) aDict['func2']() # this calls func2 which in turn calls func1. But it fails And file.py contains this: def func1(): return 1 myVar = func1() # checking that func1 exists in the scope func2 = lambda: func1() This gives an error saying " NameError: global name 'func1' is not defined ." I'm not sure what is happening here. The code in file.py is executed with an empty local namespace. Then, inside that code, a new

Python: 'import *' vs execfile

时光怂恿深爱的人放手 提交于 2019-12-12 07:47:08
问题 In some of my Django apps I'm using a settings_local.py file to override settings that are different on various environments (e.g. development, test and production). I have originally used the following code to include its contents in the settings.py : try: from settings_local import * except ImportError: sys.stderr.write("The settings_local.py file is missing.\n") DEBUG=False I have recently found the execfile function and switched to something like: try: execfile(path.join(PROJECT_ROOT,

execfile() cannot be used reliably to modify a function’s locals

只愿长相守 提交于 2019-12-11 01:15:05
问题 The python documentation states "execfile() cannot be used reliably to modify a function’s locals." on the page http://docs.python.org/2/library/functions.html#execfile Can anyone provide any further details on this statement? The documentation is fairly minimal. The statement seems very contradictory to "If both dictionaries are omitted, the expression is executed in the environment where execfile() is called." which is also in the documentation. Is there a special case when excecfile is

Run python program from another python program (with certain requirements)

别来无恙 提交于 2019-12-10 14:36:30
问题 Let's say I have two python scripts A.py and B.py . I'm looking for a way to run B from within A in such a way that: B believes it is __main__ (so that code in an if __name__=="__main__" block in B will run) B is not actually __main__ (so that it does not, e.g., overwrite the "__main__" entry in sys.modules) Exceptions raised within B propagate to A (i.e., could be caught with an except clause in A). Those exceptions, if not caught, generate a correct traceback referencing line numbers within