简体   繁体   中英

Python Creating and Importing Modules

I wrote functions that are applyRules(ch), processString(Oldstr) and named it lsystems.py And I put

import lsystems
def main():
    inst = applyRules("F")
    print(inst)
main()

and saved it as mainfunctioni

However, when I try to run mainfunctioni, it says 'applyRules' is not defined. Doesn't it work because I put import lsystems?

What should I do to work my mainfunctioni through lsystems?

You have to call it with module.function() format. So in this case, it should be called like as follows:

 inst = lsystems.applyRules("F")

You have to access all the methods from your module with the same format. For processString(Oldstr), it should be similar.

test_string = lsystems.processString("Somestring")

When you import a module using import <module> syntax, you need to access the module's contents through its namespace, like so:

import lsystems

def main():
    inst = lsystems.applyRules("F")
    print(inst)

main()

Alternatively, you can directly import the function from the module:

from lsystems import applyRules

def main():
    inst = applyRules("F")
    print(inst)

main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM