简体   繁体   English

导入本地python文件

[英]Importing local python files

I need some help importing a local python file into another file. 我需要一些将本地python文件导入另一个文件的帮助。

for example I have a file named calculator.py and want to import it into another file called file.py . 比如我有一个文件名为calculator.py并希望将其导入到另一个名为文件file.py When I run the .py file it imports (using Hydrogen for atom). 当我运行.py文件时,它会导入(使用Hydrogen作为原子)。 However, when I call a function from the file it says it can't be found. 但是,当我从文件中调用函数时,它说找不到。

from calculator.py: 来自Calculator.py:

def addition(a,b):
    c = a + b
    return(c)

In another file: 在另一个文件中:

import calculator as calc

calc.addition(2,2)

Returns: 返回值:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-d35e30d24b34> in <module>()
----> 1 calc.addition(2,2)

AttributeError: module "calculator" has no attribute "addition"

更改了分支,它似乎可以工作。

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

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