How to change Python module name?

Is this possible only if I rename the file? Or is there a variable __module__in the file to determine how its name is?

+5
source share
7 answers

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.

+8
source

"oldname.py" "import newname", , : - , 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.

+17

, , :

import foo as bar
print bar.baz
+11

import module_name, Python module_name .extension PYTHONPATH. . , , :

import module_name as new_module_name

import module_name.submodule.subsubmodule as short_name

, . DB.

import sqlite3 as sql
sql.whatever..

, . sqlite3 pysqlite

+1

__module__, , .

, , setattr , .

.

0

__module__, script , ? , sys.path.

, , oldname.pyc, , .

0

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.

0
source

All Articles