Results 1 to 7 of 7

Thread: Python 2.6: Running other .py?

  1. #1
    Join Date
    Apr 2009
    Location
    Minneapolis/St. Paul
    Beans
    90
    Distro
    Ubuntu 10.04 Lucid Lynx

    Python 2.6: Running other .py?

    Hey,
    I was wondering if there is a function, or something, that would allow me to run another .py file from the script I am running? Sort of like "import", but being able to do it more then once.

    Sort of like classes in Java or C, that allow you to break up the program into parts and have all the parts reference each other?

    I have found semi-functional ways to do this using import by:
    Code:
    import stuff
    ...(Runs the stuff file)
    del stuff
    continues program, 
    but being able to do "import stuff" again
    Anyhow, I'm just curious for an more effective method.

  2. #2
    Join Date
    Sep 2009
    Beans
    207
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python 2.6: Running other .py?

    You can write classes in python. For example,
    Code:
    #!/usr/bin/python                                                               
    
    class Test:
        one = 1
    
        def add_num(self,num):
            return self.one + num
    
    
    print Test.one
    
    tmp = Test()
    tmp.one = 2
    print tmp.add_num(3)
    Self in python is almost like this in java. As you can see I create a new Test object set its one property to two (lol). Then call the add_num function. Run this to see the results.

  3. #3
    Join Date
    Apr 2009
    Location
    Minneapolis/St. Paul
    Beans
    90
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python 2.6: Running other .py?

    I see what your saying. Its not quiet what I was thinking, but it did give me a better idea.

    I had the idea to create a module which would define the action of the whole code as a function.
    Module Greet
    Code:
    def intro(name,age):
         print "Hello",name,"who is",age,"years old!"
         print "My name is Bob!"
    The script:
    Code:
    import Greet
    myname = raw_input("Whats your name?")
    myage = raw_input("Whats your age?")
    Greet.intro(myname,myage)
    What do you think about doing something like that?

  4. #4
    Join Date
    Sep 2009
    Beans
    207
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python 2.6: Running other .py?

    Good! You made a excellent conclusion from my example, that way you can define extra classes and methods and put them in other modules and import them just like you did. Much cleaner code that way don't you think?

  5. #5
    Join Date
    Apr 2009
    Location
    Minneapolis/St. Paul
    Beans
    90
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python 2.6: Running other .py?

    Indeed, is will allow me and my friend to work on each part of the game we are building with out needing to constantly be integrating and merging code!

    Now I can build with the Crafting module, while he works on the Fighting module!

  6. #6
    Join Date
    Mar 2009
    Location
    Western Hemisphere.
    Beans
    136
    Distro
    Ubuntu

    Re: Python 2.6: Running other .py?

    If you're working on a collaborative project, I would strongly recommend you look into some sort of versioning software. It will make your life immensely easier.

    I personally like Git.

  7. #7
    Join Date
    Apr 2009
    Location
    Minneapolis/St. Paul
    Beans
    90
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python 2.6: Running other .py?

    Ill look into it!
    Thanks

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •