Hello everyone,
I was wondering, if I wanted to make my code a little smaller in one file,
is it possible for me to import another file I made?

E.G.
(Very simple e.g. just for helping understand what I mean)

Let's say I made a program that the user types in a number in an Entry (tkinter)
and presses a button that prints our what they typed in.

Could I write the Entry in one file (entry.py) and the button in another file (button.py)
and import the second file (button.py) into the first file (entry.py) and use the button?

And if so, how would I go about doing this?

Thanks in advance,
-WolfShield

Recommended Answers

All 9 Replies

Oh yes it's perfectly possible! Make sure your external script is in the same folder as your main file, then just use "import example.py" (if that doesn't work remove the ".py"). Just make sure your variables can interact between the two scripts.

An example i make some code that calulate average.

#average.py
#saved in python dir
from __future__ import division

def average(average_list):
    if isinstance(average_list, (list, tuple)):
        return sum(average_list)/len(average_list)
    else:
        return 'Input can only be list or tuple'

So import it and test it out.
Save it in python sys.path or same folder as your other .py-file.

#Check out path
>>> import sys
>>> sys.path
>>> from average import average
>>> average
<function average at 0x04D2C430>
>>> average([1,2,3,4])
2.5
>>> average(88)
'Input can only be list or tuple'
>>> average('hi')
'Input can only be list or tuple'
>>>

Thanks for the help guys!

It is correctly importing the file, but I've reached another problem.
I've been working on it a little while, I'm not trying to have you guys
write the program for me, but it's been to no avail.

In the first file (entry.py) I have:

from tkinter import *
from tkinter import ttk
import button.py

root = Tk()
root.title("Import Test")

a = ttk.Entry(root).grid(column=0, row=0)

root.mainloop()

And in the second file (button.py):

def print_it():
    print("It Works!")

b = ttk.Button(root, text="Test!", command=print_it).grid(column=0, row=1)

It's telling me that in (button.py) name 'ttk' is not defined, and name 'root' is not defined.

How can I fix this problem without creating two separate windows?

And Thanks again!

-WolfShield

You would have your main program separated from utilities and have the root window defined there:

import tkinter as tk
import button # no .py
import entry

root = tk.Tk()
root.title("Import Test")

e = entry.a(root) # need to define function
b = button.b(root) # need to define function
button.print_it()
root.mainloop()

Am I getting this wrong or do you need to import ttk again in your new script? Also, root isn't defined in the second script. I would give you an example, but I gotta go now and I'll do it for you later :)

Right SgtMe,
That is exactly the problem. But if I import 'ttk' and create a root in
the second file (button.py) then it makes two different windows.
One with the entry in it, the other with the button.

-WolfShield

Hmmm...now I'm thinking about it, it would be a hell of a lot easier to put all the GUI stuff in one file. I'd say that anything you had with your root (buttons, labels etc.) should all go in the one file. Then you could put your program logic in a different script. It really depends what your doing, but that is the easiest way.

Yep,
That's what I'm thinking. If I ever figure out a good way to do work
the importing of self made files then I'll let you all know how.

Thanks for all of your help people!

-WolfShield

This way at least it runs in Python 2.7. The logic of organizing maybe not ideal though. Usually we collect together some classes/functions as one module.

main.py

import Tkinter as tk
import button # no .py
import entry

root = tk.Tk()
root.title("Import Test")

e = entry.a(root) # need to define function
b = button.b(root) # need to define function
root.mainloop()

entry.py

import ttk
def a(root):
    return ttk.Entry(root).grid(column=0, row=0)

button.py

import ttk
def print_it():
    print("It Works!")

def b(root):
    return ttk.Button(root,
                      text="Test!",
                      command=print_it).grid(column=0, row=1)
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.