简体   繁体   English

Python创建和导入模块

[英]Python Creating and Importing Modules

I wrote functions that are applyRules(ch), processString(Oldstr) and named it lsystems.py And I put 我编写了applyRules(ch),processString(Oldstr)函数并将其命名为lsystems.py

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. 但是,当我尝试运行mainfunctioni时,它说未定义'applyRules'。 Doesn't it work because I put import lsystems? 因为我放置了导入系统,所以行不通吗?

What should I do to work my mainfunctioni through lsystems? 我应该怎么做才能通过lsystems运行mainfunctioni?

You have to call it with module.function() format. 您必须使用module.function()格式进行调用。 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. 对于processString(Oldstr),它应该相似。

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 <module>语法import <module> ,需要通过其名称空间访问模块的内容,如下所示:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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