getting started, .py file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nibiery@hotmail.com

    #1

    getting started, .py file

    I am just getting started with Python, and I think I may be thinking
    about it wrong. I'd like to be able to work interactively with some
    code that I've got in a file. The only interpreted language I have much
    experience with is Tcl/Tk, and in that I would use "source file.tcl" in
    the console to load my source. Is there a similar command in python? I
    know I can run my file as a script, but since I'm just experimenting
    with how the language works, I want to have more flexibility to be able
    to interactively check the contents of variables and define one piece
    at a time. Am I missing something obvious, or am I not thinking about
    Python properly?

    Thanks,
    Ingrid

  • Sam Pointon

    #2
    Re: getting started, .py file

    python -i source_file.py
    will do what you want.

    -Sam

    Comment

    • Steve Holden

      #3
      Re: getting started, .py file

      nibiery@hotmail .com wrote:[color=blue]
      > I am just getting started with Python, and I think I may be thinking
      > about it wrong. I'd like to be able to work interactively with some
      > code that I've got in a file. The only interpreted language I have much
      > experience with is Tcl/Tk, and in that I would use "source file.tcl" in
      > the console to load my source. Is there a similar command in python? I
      > know I can run my file as a script, but since I'm just experimenting
      > with how the language works, I want to have more flexibility to be able
      > to interactively check the contents of variables and define one piece
      > at a time. Am I missing something obvious, or am I not thinking about
      > Python properly?
      >[/color]
      Yes. If your code is in "mycode.py" then start up the interactive
      interpreter and enter

      import mycode

      at the ">>>" prompt. You will now find that names you have defined in
      the mycode module can be referred to as mycode.this, mycode.that and so
      on, allowing you to interactively play with the features of you module.

      A related technique (possibly acceptable for interactive testing but
      definitely *not* recommended for production use) is

      from mycode import *

      which puts the names directly into the importing module's (that is to
      say the interactive interpreter's) namespace and allows you to refer to
      them directly as this, that, and so on.

      This interactive design is one on the Python programmer's secret
      weapons, as it makes it so easy to try things out and be sure you have
      got your Python correct.

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC www.holdenweb.com
      PyCon TX 2006 www.python.org/pycon/

      Comment

      • Peter Hansen

        #4
        Re: getting started, .py file

        nibiery@hotmail .com wrote:[color=blue]
        > I am just getting started with Python, and I think I may be thinking
        > about it wrong. I'd like to be able to work interactively with some
        > code that I've got in a file. The only interpreted language I have much
        > experience with is Tcl/Tk, and in that I would use "source file.tcl" in
        > the console to load my source. Is there a similar command in python? I
        > know I can run my file as a script, but since I'm just experimenting
        > with how the language works, I want to have more flexibility to be able
        > to interactively check the contents of variables and define one piece
        > at a time. Am I missing something obvious, or am I not thinking about
        > Python properly?[/color]

        Note that one useful thing you can do if you take Steve's first approach
        ("import mycode") is to do "reload(mycode) " after you've made changes in
        a text editor and resaved your mycode.py file. That will reload the
        module from the source, picking up whatever changes you've made. Handy
        for some kinds of quick experimentation ... (though often just typing
        stuff at the prompt and then integrating with your source after you've
        got it working is a more efficient approach).

        -Peter

        Comment

        • Ingrid

          #5
          Re: getting started, .py file

          Thanks everyone. That's exactly what I was looking for, but I still
          can't seem to make it work. I've got the interpreter starting in
          "C:\Program Files\Python2.4 ", and my code is in "C:\Documen ts and
          Settings\Ingrid \My Documents". So, I did:
          import os
          os.chdir("C:\\D ocuments and Settings\\Ingri d\\My Documents")
          from mycode import *

          and I get the error message:
          Traceback (most recent call last):
          File "<pyshell#2 9>", line 1, in -toplevel-
          import mycode
          ImportError: No module named mycode

          I'm sure I'm missing something obvious again, but I don't see anything
          in the import documentation about directories.

          Ingrid

          Comment

          • Ingrid

            #6
            Re: getting started, .py file

            I found it! I needed to set sys.path ("sys.path.appe nd("c:\\documen ts
            and settings\\my documents\\ingr id")")

            Ingrid

            Comment

            • Steven D'Aprano

              #7
              Re: getting started, .py file

              Ingrid wrote:
              [color=blue]
              > Thanks everyone. That's exactly what I was looking for, but I still
              > can't seem to make it work. I've got the interpreter starting in
              > "C:\Program Files\Python2.4 ", and my code is in "C:\Documen ts and
              > Settings\Ingrid \My Documents". So, I did:
              > import os
              > os.chdir("C:\\D ocuments and Settings\\Ingri d\\My Documents")
              > from mycode import *[/color]

              I see that Ingrid found the solution to this problem.
              But I wonder, should Python support importing absolute
              pathnames?

              I've trying googling, but either my google skills are
              lacking or nobody has ever thought of doing import
              "C:\directory\m odule" before.


              --
              Steven.

              Comment

              Working...