简体   繁体   English

为什么我不能将我的模块导入到我的主代码中?

[英]Why can't I import my module into my main code?

I wrote some code (named exercise 2) where I define a function (named is_divisible) and it has worked perfectly.我编写了一些代码(命名为练习 2),其中定义了一个函数(命名为 is_divisible)并且它运行良好。

Afterwards to learn how to import functions, I wrote the same code but without the defined function, and created a second module (named is_divisible).之后为了学习如何导入函数,我编写了相同的代码但没有定义函数,并创建了第二个模块(名为 is_divisible)。 But whenever I import this module into the original "exercise 2" I get但是每当我将此模块导入原始的“练习 2”时,我就会得到

No module named 'is_divisible'没有名为“is_divisible”的模块

I have checked that both python files are in the same folder, the name of the file is correct, and I know the code is well written because it has worked before and it is from a lecturer's of mine.我已经检查过两个 python 文件是否在同一个文件夹中,文件名是正确的,我知道代码写得很好,因为它以前工作过,而且它来自我的讲师。 I have also attempted to name the module and the function differently and to instead write:我还尝试以不同的方式命名模块和函数,而是这样写:

from divis import is_divisible

but this was also unsuccessful.但这也没有成功。

Where am I going wrong?我哪里错了? I will leave the code below:我将在下面留下代码:

import random
import math
import numpy as np

random_list=[]

for i in range (0,5):
    r=random.randint(0,10)
    random_list.append(r)

print(random_list) #five numbers from 0 to 10 are chosen and appended to a list


new_result=[print('right' for x in random_list if round(np.cosh(x)**2 - np.sinh(x)**2,2) == 1] 
#checking the numbers following a maths rule

import is_divisible  #trying to import the function is_divisible

divisor=3 
idx = is_divisible(random_list, divisor)
for i in idx:
    print(f'Value {random_list[i]} (at index {i}) is divisible by {divisor}')

the code for the function is_divisible is:函数is_divisible的代码是:

def is_divisible(x, n):
""" Find the indices of x where the element is exactly divisible by n.

Arguments:
x - list of numbers to test
n - single divisor

Returns a list of the indices of x for which the value of the element is
divisible by n (to a precision of 1e-6 in the case of floats).

Example:
>>> is_divisible([3, 1, 3.1415, 6, 7.5], 3)
[0, 3]

"""

r = []
small = 1e-6
for i, m in enumerate(x):
    if m % n < small:
        r.append(i)
return r

I know this question has been answered multiple times, but none of the answers seem to work for me or maybe I am not doing it correctly.我知道这个问题已被多次回答,但似乎没有一个答案对我有用,或者我没有正确回答。

Generally, when you type import <Module> the module is the name of the file.通常,当您键入import <Module> ,模块是文件的名称。 So, if you had the function is_divisible inside a Python file named a.py , then to import it you will write from a import is_divisible .因此,如果您在名为a.py的 Python 文件中有函数is_divisible ,那么要导入它,您将从from a import is_divisible If instead, you would like to import the whole file, then you'd write import a.py , then to use the function you would use a.is_divisible(random_list, divisor) .相反,如果您想导入整个文件,那么您可以编写import a.py ,然后要使用该函数,您将使用a.is_divisible(random_list, divisor)

You should also make sure that both files are in the same folder.您还应该确保两个文件都在同一个文件夹中。

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

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