View Full Version : [SOLVED] Python - how to use a custom function, located into another Python file ?
credobyte
September 8th, 2009, 01:45 PM
Ok, so - is it even possible to achieve this ? If so, what exactly I need to do to make it work ?
core.py
def say(str):
print str
main.py
say("It works!")
Tony Flury
September 8th, 2009, 01:50 PM
yes - so long as core.py is in your PYTHON_PATH (your current directory should be sufficient) then
import core
say("It works!")
will work.
DaithiF
September 8th, 2009, 01:53 PM
either:
import core
core.say("hello")
or
from core import say
say("hello")
credobyte
September 8th, 2009, 01:56 PM
Thank you, however, something doesn't work right ..
Once I execute my main.py, all I get is a core.pyc ( new file ) and an error, stating that core is not defined.
Ehh ? :-k
UPDATE :: DaithiF, #1 example works like a charm .. others don't. Thank you :)
Bachstelze
September 8th, 2009, 01:56 PM
yes - so long as core.py is in your PYTHON_PATH (your current directory should be sufficient) then
import core
say("It works!")
will work.
No, it will not. ;)
import core
core.say("It works!")
will.
Never mind my first message, my brain is tired from the 4 hours of math this morning.
DaithiF
September 8th, 2009, 02:01 PM
Thank you, however, something doesn't work right ..
Once I execute my main.py, all I get is a core.pyc ( new file ) and an error, stating that core is not defined.
Ehh ? :-k
UPDATE :: DaithiF, #1 example works like a charm .. others don't. Thank you :)
my #2 example works too! :)
the .pyc file is just a compiled version of the core.py module, which python creates as a convenience to allow it to import the module more efficiently next time.
credobyte
September 8th, 2009, 02:05 PM
my #2 example works too! :)
the .pyc file is just a compiled version of the core.py module, which python creates as a convenience to allow it to import the module more efficiently next time.
Ok, I see. Thanks for an explanation ( .pyc ) :)
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.