Hey guys, I was wondering if there is a way to include other python programs in your python program. Sorry if there is another thread about this I looked and was unable to find one.

Recommended Answers

All 8 Replies

hmm, what do you mean by include? you can import into your programs using:

import 'module name'

Here is an example ...

# save this code as dec2bin.py so you can access the function decimal2binary()
# in other Python programs

def decimal2binary(n):
    '''convert decimal (base 10) integer n to binary (base 2) string bStr'''
    bStr = ''
    if n < 0:  raise ValueError, "must be a positive"
    if n == 0: return '0'
    while n > 0:
        bStr = str(n % 2) + bStr
        n = n >> 1
    return bStr

# This test runs when used as a standalone program, but not as an imported module.
# Let's say you save this module as dec2bin.py and use it in another program
# when you import dec2bin the __name__ namespace would now be dec2bin and the
# test would be ignored
if __name__ == '__main__':
    print decimal2binary(255)  # test result = 11111111

... and here is how you would use it ...

# using your custom made module dec2bin.py
# put it the same folder so Python can find it

import dec2bin

print dec2bin.decimal2binary(128)  # 10000000

What I want to do is make a program that shows a list, like say:
[1] --- Guess Number Game
[2] --- Calendar

And I code the guess number game and calendar as seperate .py programs. I would like to make a program that shows that list and when the user picks one, the program will start to run that program.

I would just make the code be all together in one big program. But I am just learning to program in python and I personally find it more organised to call txt files instead of having huge paragraphs in the code, and would also like to make seperate programs that can be called on. I find it easier to keeop track of everything.

Another thing I need help with, is how do i get the program to loop back the begining at the end

Another thing I need help with, is how do i get the program to loop back the begining at the end

Here is a way suggested in a different thread.

You put program code in a loop and designate one key to break out of loop:

while True:
 
    # this is an example of your program:
    print
    print "My program!  Really!"
    print
 
    choice = raw_input("Enter r (repeat) or q (quit): ")
    if 'q' in choice:
        break  # exits the while loop, any other key repeats loop

I put an example up at:
http://www.daniweb.com/techtalkforums/post223717-58.html

This one uses the option menu within a infinite while loop with a quit option to break out of the loop. The games are called with functions.

Instead of functions you can use execfile("mygame.py") to execute external Python programs you have written and saved in the same directory. If they are in different directories, you have to give the full path.

Thanks alot guys. These things may have seemed very simple to you. But for the life of me I couldnt find them anywhere, and it helped me ALOT.

Thanks Again guys :D

when i was first playing about with python (wait... i still am) anyway, a good website is: http://www.bembry.org/
just click on the python section, it has a great simple pdf tut and some quick notes aswell

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.