I'm having some trouble calling a function which is within a class in python.
Here is my folder hierarchy.
~/Code/program/main.py
~/Code/program/dc_functions/dcfunc.py
~/Code/program/dc_functions/init.py
Basically, I want to use a function from dcfunc.py inside of main.py. How would I do this?
Relevant contents of dcfunc.py:
import subprocess, string, os, sys
class dcfunc:
#Create raw Audio track(Part of Dreamcast Disc format) + Burn track to disk.
def __init__(self):
self.self = "self"
def burnaudiotrack(device):
**CODE***
Thanks for any suggestions!
asked May 13, 2011 at 23:57
Sinthet
9032 gold badges8 silver badges13 bronze badges
1 Answer 1
You need your init.py file to be named __init__.py
then use
from dc_functions.dcfunc import function_name
And you'll have acccess to the function.
answered May 14, 2011 at 0:00
Winston Ewert
45.2k10 gold badges70 silver badges86 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
Sinthet
When I try to import it like that, then try to use it, I get a NameError. Do I just call it as if it were local? For example : function()
bluepnume
Yes. You're importing the name directly into the local namespace, so you can call it directly.
Sinthet
'from dc_functions.dcfunc import *' This is what I added into my main.py. When I try to call any function within it from main.py, the interpreter tells me the name is not defined. Maybe something in init.py is wrong. I have "import dcfunc" there, and nothing else.
Sinthet
File "./main.py", line 44, in <module> burn() NameError: name 'burn' is not defined
Winston Ewert
You have all of the functions in that file inside a class. In order for what I told you to work they have to be outside of the class. The class in that file doesn't make any sense. Probably you should just take all of the methods out of the class.
|
lang-py
self.self = "self"doesn't have sense. Anddef burnaudiotrackshould have as first argumentself. You are saying that you want to use a function fromdcfunc.py, are you sure? Probably you want to call a method instead.