Importing a module with dashes.

Steven D. Majewski sdm7g at virginia.edu
Tue Oct 24 15:56:18 EDT 2000


On 24 Oct 2000, Igor V. Rafienko wrote:

> 
> I have a module that contains a dash in its name. import severely
> dislikes it, and although I can try something like:
> 
> __import__( "my-module" )
> 
> it looks like a wrong thing to do. Any clues as to what is the proper
> way of dealing with this?
> 

Yes: rename the module to "my_module" or "MyModule" .

The string "my-module" has a different meaning to python:

>>> my  = 1
>>> module = 2
>>> my-module
-1
>>>

According to the grammar, it's not a valid identifier. So you can't say:

>>> my-module = __import__( "my-module" ) 
SyntaxError: can't assign to operator


You will have to give it a valid name somewhere.
You would have a similar problem with the following: 

>>> import new
>>> unspoken = new.module( '|#%@!&*!' )
>>> unspoken
<module '|#%@!&*!' (built-in)>
>>> sys.modules[unspoken.__name__] = unspoken 

( Note to self: remember this for the next obfuscated python contest! ;-)


---|  Steven D. Majewski   (804-982-0831)  <sdm7g at Virginia.EDU>  |---
---|  Department of Molecular Physiology and Biological Physics  |---
---|  University of Virginia             Health Sciences Center  |---
---|  P.O. Box 10011            Charlottesville, VA  22906-0011  |---
		"All operating systems want to be unix, 
		 All programming languages want to be lisp." 





More information about the Python-list mailing list