Mads Andersen
Mads Andersen

Reputation: 139

include A python file that exisist in the project

Im totaly new to python so i am in the absolute beginner stage. I know alot PHP so my mind think in the PHP way when i am trying stuff with Python.

I have a python project that contains a:

test.py
Includes(folder)
- math.py

The test.py contains the code:

import MySQLdb

db = MySQLdb.connect(host="127.0.0.1",
                 user="root", 
                  passwd="", 
                  db="python")


from Tkinter import *




root = Tk()
root.title("test")
root.geometry("500x500")
window = Frame(root)
window.grid()
label = Label(text="")
label.grid()
def click():
cur = db.cursor() 
query = cur.execute("SELECT * FROM names")
result = cur.fetchall()
label["text"] = result[0]

b1 = Button(window, text = "Click me!",command=click)
b1.grid()   
root.mainloop()

How do i include the math.py file so i can use the class inside. I know in PHP it's:

include("indcludes/math.py");

But how do you make the same thing in Python, if its posible at all ;)

Ty for your time.

Upvotes: 0

Views: 48

Answers (2)

ChronosKey
ChronosKey

Reputation: 66

Create an empty file called __init__.py in your includes folder.

$ touch includes/__init__.py

In your client code test.py, import the math.py class through

from includes import math

Upvotes: 1

hemanth
hemanth

Reputation: 1043

try adding this

from includes import math

For above line to work you need to create an empty file in the includes folder with name __init__.py. This will make the folder a package (ref)

Upvotes: 1

Related Questions