Is this possible only if I rename the file? Or is there a variable __module__in the file to determine how its name is?
__module__
Yes, you must rename the file. It is best after you do this to delete the compiled files oldname.pycand oldname.pyo(if any) from your system, otherwise the module will be imported under the old name.
oldname.pyc
oldname.pyo
"oldname.py" "import newname", , : - , sys.modules . . :
sys.modules
# this is in file 'oldname.py' ...module code...
:
# inject the 'oldname' module with a new name import oldname import sys sys.modules['newname'] = oldname
import newname.
import newname
, , :
import foo as bar print bar.baz
import module_name, Python module_name .extension PYTHONPATH. . , , :
import module_name
module_name
.extension
import module_name as new_module_name
import module_name.submodule.subsubmodule as short_name
, . DB.
import sqlite3 as sql sql.whatever..
, . sqlite3 pysqlite
sqlite3
pysqlite
__module__, , .
, , setattr , .
.
__module__, script , ? , sys.path.
sys.path
, , oldname.pyc, , .
I am having a problem with bsddb. I was forced to install the bsddb3 module, but hundreds of scripts imported bsddb. Instead of changing the import in all of them, I extracted the bsddb3 egg and created a soft link in the package sites directory, so that both "bsddb" and "bsddb3" were the same for python.