Personal tools
You are here: Home Forum How to use Salome? Creating Function in Separate python file

Creating Function in Separate python file

Up to How to use Salome?
  • Creating Function in Separate python file

    Posted by pichon at January 16. 2007
    Hi,

    I would like to create my own function and store it as python independent script file.
    How do I call it from another python script?

    Thank You
    • Re: Creating Function in Separate python file

      Posted by Erwan ADAM at January 16. 2007
      pichon wrote:
      Hi,

      I would like to create my own function and store it as python independent script file.
      How do I call it from another python script?

      Thank You


      from my_file_without_dot_py import my_function

      my_function( ... )


      • Re: Creating Function in Separate python file

        Posted by pichon at January 16. 2007
        Thank You,

        from myFunc_script import myFunc_function
         - works fine.

        When I use:   import myFunc_scrip
        i get the following error:

        Traceback (most recent call last):
          File "", line 1, in ?
          File "/salome/code/go3.py", line 50, in ?
            myFunc_function()
        TypeError: 'module' object is not callable


        The purpose was to store 2 or more functions in a separate script, and
        to call it by import.

        Thank You
        • Re: Creating Function in Separate python file

          Posted by pichon at January 16. 2007
          Hi,
          The answer could be:

          from my_python_script import *

          this will provide access to all functions.
          • Re: Creating Function in Separate python file

            Posted by Erwan ADAM at January 17. 2007
            pichon wrote:
            Hi,
            The answer could be:

            from my_python_script import *

            this will provide access to all functions.


            Personnaly, I NEVER use the 'import *' feature
            'cause it becomes very hard to analyse when there
            is a bug ... for instance you begin by :

            from foo import *

            to use the content of foo, then you need to use something
            in bar.py so you add

            from bar import *

            and so on ... Result, when you read your script two weeks
            later, you cannot recall what comes from foo and what comes
            from bar ...

            and imagine that there is a fonction with same name in foo.py
            and bar.py ...

            My philosophy is to use :

            from foo import something

            just before using something (it facilitate the copy / paste of code)

            you can use

            import foo
            foo.something

            The only exception I use is :

            from qt import *

            'cause all names defined in qt begin by Q...

            Regards,

                            E.A.
        • Re: Creating Function in Separate python file

          Posted by Erwan ADAM at January 17. 2007
          pichon wrote:
          Thank You,

          from myFunc_script import myFunc_function
           - works fine.

          When I use:   import myFunc_scrip
          i get the following error:

          Traceback (most recent call last):
            File "", line 1, in ?
            File "/salome/code/go3.py", line 50, in ?
              myFunc_function()
          TypeError: 'module' object is not callable


          The purpose was to store 2 or more functions in a separate script, and
          to call it by import.

          Thank You


          import myFunc_script
          myFunc_script.myFunc_function()
Powered by Ploneboard
Document Actions