Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, how to restore the function of os.chdir() if os.chdir = 'path' has been implemented

Tags:

python

In path setup, I wrongly wrote the code: os.chdir = '\some path', which turns the function os.chdir() into a string. Is there any quick way to restore the function without restarting the software? Thanks!

like image 653
jingweimo Avatar asked Sep 28 '16 01:09

jingweimo


1 Answers

Kicking os out of the modules cache can make it freshly importable again:

>>> import sys, os
>>> os.chdir = "d'oh!"
>>> os.chdir()
TypeError: 'str' object is not callable
>>> del sys.modules['os']
>>> import os
>>> os.chdir
<function posix.chdir>
like image 129
wim Avatar answered Sep 28 '22 10:09

wim