Questions tagged [execfile]

execfile is a Python 2.x. built-in function that executes Python scripts from files.

84 questions
62
votes
4 answers

Alternative to execfile in Python 3?

Python 2 had the builtin function execfile, which was removed in Python 3.0. This question discusses alternatives for Python 3.0, but some considerable changes have been made since Python 3.0. What is the best alternative to execfile for Python 3.2,…
Matt Joiner
  • 100,604
  • 94
  • 332
  • 495
17
votes
3 answers

Stop execution of a script called with execfile

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…
JcMaco
  • 1,208
  • 3
  • 16
  • 31
11
votes
3 answers

Python: 'import *' vs execfile

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…
Berislav Lopac
  • 14,056
  • 6
  • 62
  • 75
7
votes
1 answer

Difference between import and execfile

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?
Old Geezer
  • 10,411
  • 21
  • 87
  • 156
7
votes
2 answers

Custom Scheduler to have sequential + semi-sequential scripts with timeouts/kill switches?

Below is a big section of my code and basically if you scroll down to the execute_subscripts() function you can see I've got two scripts running via execfile which work beautifully, they show prints, they save traceback errors to an error file. I'm…
Ryflex
  • 4,369
  • 22
  • 65
  • 135
7
votes
1 answer

Python: execfile from other file's working directory?

I have some code that loads a default configuration file and then allows users to supply their own Python files as additional supplemental configuration or overrides of the defaults: # foo.py def load(cfg_path=None): # load default…
Kyle Kaitan
  • 1,731
  • 3
  • 19
  • 28
6
votes
2 answers

Python modules: functions calling each other

I have got myself very confused. I have a set of python functions, all of which I've bunged together in a file called (let's say) useful.py. I can then read the module into my ipython with import useful as uf and then I can access individual…
Alasdair
  • 1,208
  • 1
  • 13
  • 26
5
votes
2 answers

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

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__' ) #…
george
  • 625
  • 2
  • 7
  • 19
5
votes
2 answers

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

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…
BrenBarn
  • 210,788
  • 30
  • 364
  • 352
4
votes
2 answers

[Python]Encoding and execfile

I am trying to do something like that using python 2.4: #!/usr/bin/python # -*- coding: utf-8 -*- afile = unicode('C:\\國立國語院.py', 'UTF-8') execfile(afile.encode("UTF-8",'replace')) And I receive this error: IOError: [Errno 2] No such file or…
Kvoste
  • 41
  • 2
4
votes
2 answers

execFile not being called

I'm having a problem and not even really sure where to begin troubleshooting. I'm using a slightly modified mocha-casperjs. CasperJS is a wrapper for PhantomJS. I'm trying to integrate Growl notifications on completion of my tests. I can execute…
thekevinscott
  • 4,661
  • 8
  • 38
  • 54
4
votes
2 answers

Why doesn't import prevent NameError in a python script run with execfile()?

I looked at a number of existing questions about NameError exceptions when scripts are run with exec statements or execfile() in Python, but haven't found a good explanation yet of the following behavior. I want to make a simple game that creates…
William Knight
  • 650
  • 8
  • 15
3
votes
0 answers

Is there ever a good reason to put python code in a txt file?

I noticed a file include.txt in every project I open. The file contains python code: from aws_xray_sdk.core import patch_all import sentry_sdk from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration # pragma: no…
3
votes
1 answer

Execute python files from a list

I have made a search of python scripts in some subfolders using os.walk() and endswith(file.py), and put them in a list. Now I'm trying to execute them. So I have a list of the type: pylist = ['./1/file1.py', './2/file2.py', ...] and I have tried…
EternalGenin
  • 363
  • 3
  • 11
3
votes
1 answer

Executing an exe with parameters in nodejs

I wanted to execute an exe using node js. This is how the command looks in command prompt of windows: oplrun -D VersionId=3458 -de "output.dat" "Test.mod" "Test.dat" This runs fine and I get the output in output.dat file. Now, I wanted to execute…
Ajith
  • 73
  • 1
  • 6
1
2 3 4 5 6