Alternatywa dla execfile w Pythonie 3? [duplikat]
To pytanie ma już odpowiedź tutaj:
- Jaka jest alternatywa dla execfile w Pythonie 3? 12 odpowiedzi
Python 2 miał wbudowaną funkcję execfile
, który został usunięty w Pythonie 3.0. to pytanie omawia alternatywy dla Pythona 3.0, ale niektóre znaczące zmiany zostały wprowadzone od czasu Pythona 3.0.
Jaka jest najlepsza alternatywa dla execfile
dla Pythona 3.2 i przyszłego Pythona 3.wersje x ?
41
4 answers
Skrypt 2to3
(również ten w Pythonie 3.2) zastępuje
execfile(filename, globals, locals)
By
exec(compile(open(filename, "rb").read(), filename, 'exec'), globals, locals)
To wydaje się być oficjalne zalecenie. 53
Author: Sven Marnach,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-13 09:46:17
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-13 09:46:17
execfile(filename)
Można zastąpić
exec(open(filename).read())
Który działa we wszystkich wersjach Pythona
40
Author: Lennart Regebro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-15 12:29:38
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-15 12:29:38
W Python3.x jest to najbliższa rzecz, jaką mogłem wymyślić do bezpośredniego wykonania pliku, który pasuje do działania python /path/to/somefile.py
.
Uwagi:
- używa odczytu binarnego, aby uniknąć problemów z kodowaniem
- Garenteed to close the file (Python3.x ostrzega przed tym)
- definiuje
__main__
, niektóre skrypty zależą od tego, aby sprawdzić, czy ładują się jako moduł, czy nie dla np.if __name__ == "__main__"
- ustawienie
__file__
jest ładniejsze dla komunikatów o wyjątkach i niektóre skrypty używają__file__
, Aby uzyskać ścieżki innych plików względem nich.
def exec_full(filepath):
import os
global_namespace = {
"__file__": filepath,
"__name__": "__main__",
}
with open(filepath, 'rb') as file:
exec(compile(file.read(), filepath, 'exec'), global_namespace)
# execute the file
exec_full("/path/to/somefile.py")
7
Author: ideasman42,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-15 06:07:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-15 06:07:43
Standard runpy.run_path jest alternatywą.
2
Author: eudoxos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-18 20:54:45
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-18 20:54:45