I'm not sure, if this is related to Can't find module when running python script from another python script.
However, if I run my python script directly I have no issues, but when I start it from another script, I get an error:
'Non-existent config key: model.kwargs.backbone_config.kwargs.input_height'
Even though the key can be found if I run the file directly.
However, if I try to execute the following code:
try:
config.merge_from_file('{base}/HoHoNet/config/s2d3d_sem/HOHO_depth_dct_efficienthc_TransEn1_h1024_fold1_resnet101rgb.yaml'.format(base = os.getcwd()))
except Exception as e:
print(e)
return 1
where my config is taken from yacs.config.
Related
I am running multiple scripts in sequence according to the list I and the following executable. However, when one of the script in folder (say, 2) runs into an error, it terminates instead of moving to folder 3. Basically I want the executable to move onto the next script if there is an error in the present script. How do I do this?
I=[1,2,3]
for i in I:
exec(open(rf"C:\5100 nodes\{i}\5100_beta_0.01_50.0_1.0ND_3.py").read())
The error encountered while running script in folder 2 is
File "<string>", line 618, in <module>
ValueError: max() arg is an empty sequence
You could use a try-except block.
I=[1,2,3]
for i in I:
try:
exec(open(rf"C:\5100 nodes\{i}\5100_beta_0.01_50.0_1.0ND_3.py").read())
except Error as e:
print(e)
I've written a function which reads and runs a python script, then sends it's output to a text file.
I'm trying to get it to write a simple string, or the error in question, to the text file if the script it ran is broken/doesn't work.
Below is the code in question:
file_dir = a_dir[0]
file_name = a_dir[1][:-3]
with open(f'{self.output_directory}\\output_{file_name}.txt', 'w') as f:
try:
subprocess.call(
[sys.executable, file_dir], stdout=f)
except:
f.write("An error occured with the script")
The first part of it works fine - it does run a functioning file and writes the output.
Do I need to be more specific with the error exception? Any help would be greatly appreciated!
If your code works fine and sys.executable is being run then there will be no exception and so your f.write code won't be run. If there is an error in a program you run using subprocess this doesn't propagate to an exception in a program you run it from. You'd have to know something about this program to know that there was an error you could look at the [returncode][1] from the subprocess.call function call. Another option is that instead of running a new python interpreter you could load the module yourself and then run code from within it using try except blocks. If you can't rely on any structure to the file then you could read the file as text and then run the code within it using eval or exec within a try except structure, that being said if you don't know anything about the file in advance it is likely a massive security flaw for you to be executing it at all let alone within the context of your running application.
**late edit read the file as text vice test which was a typo
[1]: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.returncode
I am trying to create a function in my automation python code that will open an editor or other text viewer with the test log file while it's been written.
My automation is based on pytest so I am executing my tests with pytest test_name.py command.
I searched in the Internet and found that with subprocess python package I can do it but when I wrote the following code:
def __open_visual_logging(self):
try:
return subprocess.Popen(["vim",self._filename])
except Exception:
return None
Nothing is happening when I'm running my tests.
(The return value is 1)
Is there other way to do this (packages etc.) ? or what am I doing wrong?
I'm trying to generate from a simple python named test.py script the c code,
the command that I'm using is:
python rpython --source ~/work/test.py.
I don't get what I'm doing wrong because from the exception that I received in the output I don't understand much :
Exception: file '/work/test.py' is not a valid targetxxx.py.
Any idea what should i do to avoid the exception?
I had a similar issue; what fixed it for me was adding the target function to my main script:
def target(*args):
return entry_point, None
hope it helps :)
I am using python 2.4 for a program which imports scripts from the internet and executes them so a script could be changed by the author and the user wouldn't have to re-download the script.
This is the part of the program that downloads the script:
def downloadScript(self,script):
myfile=open('#A file path/'+script['name']+'.txt','w')
try:
downloadedScript=urllib.urlopen(script['location']).read()
except:
#raise error
return
myfile.write(downloadedScript)
myfile.close()
def loadScript(self):
if not self.scriptCurrentlyLoaded:
script=self.scripts[self.scroller.listPos]
if script['location']=='None':
#raise error
return
self.downloadScript(script)
myfile=open('#A file path/'+script['name']+'.txt','r')
for line in myfile:
if line.startswith('from') or line.startswith('import'):
exec(line.strip()) #This was added because of the name errors
#being produced but to no affect
myfile.close()
execfile('#A file path/'+script['name']+'.txt')
self.scriptCurrentlyLoaded=True
self.scriptLoaded=script
else:
#raise error
The very odd thing is that when I run
execfile(script path)
outside of the function, after the script has been downloaded, the script is executed correctly. But trying to run the loadScript function raises name errors in the script even though the names have been imported in the script and before the execfile which I find very odd.
So my question is: Am I using a very bad method to download and execute these scripts?
Sorry if this question was answered before but I can't seem to find anyone else who is trying to run python scripts by downloading them from the internet.
Edit: adding globals as another argument to the execfile has seemed to fix the problem for now. I don't know if any other problems will occur later though.
In R you can simply 'source(url)'. Here's the closest I have found so far in python:
import urllib
(fn,hd) = urllib.urlretrieve('http://host.com/file.py')
execfile(fn)