Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named 'x' when reloading with os.execl()

I have a python script that is using the following to restart:

python = sys.executable
os.execl(python, python, * sys.argv)

Most the time this works fine, but occasionally the restart fails with a no module named error. Examples:

Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 68, in <module>
import os
File "/usr/lib/python2.7/os.py", line 49, in <module>
import posixpath as path
File "/usr/lib/python2.7/posixpath.py", line 17, in <module>
import warnings
File "/usr/lib/python2.7/warnings.py", line 6, in <module>
import linecache
ImportError: No module named linecache

Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 68, in <module>
import os
 File "/usr/lib/python2.7/os.py", line 49, in <module>
import posixpath as path
 File "/usr/lib/python2.7/posixpath.py", line 15, in <module>
import stat   
ImportError: No module named stat

Edit: I attempted gc.collect() as suggested by andr0x and this did not work. I got the same error:

Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 68, in <module>
import os
File "/usr/lib/python2.7/os.py", line 49, in <module>
import posixpath as path
ImportError: No module named posixpath

Edit 2: I tried sys.stdout.flush() and im still getting the same error. I've noticed I am only every getting between 1-3 successful restarts before an error occurs.

like image 945
Martyn Avatar asked Nov 19 '13 14:11

Martyn


1 Answers

I believe you are hitting the following bug:

http://bugs.python.org/issue16981

As it is unlikely that these modules are disappearing there must be another error that is actually at fault. The bug report lists 'too many open files' as prone to causing this issue however I am unsure if there are any other errors which will also trigger this.

I would make sure you are closing any file handles before hitting the restart code. You can also actually force the garbage collector to run manually with:

import gc
gc.collect()

http://docs.python.org/2/library/gc.html

You can try using that before hitting the restart code as well

like image 189
imandrewd Avatar answered Oct 13 '22 02:10

imandrewd