import help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • placid

    #1

    import help

    Hi all,

    How do i import a module that has an hypen/dash (-) in its name ? I get
    a SytaxError exception

    Cheers

  • Gary Herron

    #2
    Re: import help

    placid wrote:
    Hi all,
    >
    How do i import a module that has an hypen/dash (-) in its name ? I get
    a SytaxError exception
    >
    Cheers
    >
    >
    You can't do that directly. However, the internals of the import
    mechanism are available to the programmer through a module named imp. It
    allows you to import a file whose name is specified in a string -- that
    should work for you.

    Gary Herron

    Comment

    • Rob Wolfe

      #3
      Re: import help


      placid wrote:
      Hi all,
      >
      How do i import a module that has an hypen/dash (-) in its name ? I get
      a SytaxError exception
      You can use function __import__.
      >>print open("-test.py").read( )
      def fun2():
      return "Hello from -test !"
      >>import -test
      File "<stdin>", line 1
      import -test
      ^
      SyntaxError: invalid syntax
      >>__import__( "-test", globals(), locals(), [])
      <module '-test' from '-test.pyc'>

      But here is another problem:
      >>-test.fun2()
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      NameError: name 'test' is not defined
      >>import sys
      >>sys.modules["-test"].fun2()
      'Hello from -test !'

      Regards,
      Rob

      Comment

      Working...