关于 Python Import

2024.9.4 自己做了个数模题目,虽然题目也被我简化了很多,但是,但是代码梳理起来还是挺烦的。

现在,我要动脑筋,利用好 python import,或者计算机模块化的思想。

 

方案一:CSDN  

refer https://blog.csdn.net/zengliguang/article/details/138122464

 

方案二:自己设置 sys.path() + import myfilename.py

 

方案三:关于 sys.path() 

https://www.cnblogs.com/hls-code/p/15337302.html

 

最终我使用了 2 个 SCRIPT!

 

2024.9.5

 

我想了想,关键问题没写。为了解决【理清思路】,我按照问题,一个问题一个 SCRIPT。

为了增加专业知识,本地 import 我们还是 Cracking Down 一下吧!!!

 

2024.9.8

1. 首先,知道你的 python 位置!

(1)pwd 

显示当前路径,称为【路径1】。

 

(2)三行命令:

import sys
print(sys.version)
print(sys.executable)

显示你正在使用的 python 路径。注意,很多时候都会安装很多个 python。用这个命令,确定你正在用的这个 path,称为【路径2】。

 

2.编写你的 .py 函数,这里命名为 useful_functions.py 

参考:https://blog.csdn.net/tyro_java/article/details/80739247

# useful_functions.py
def mean(num_list):
    return sum(num_list) / len(num_list)


def add_five(num_list):
    return [n + 5 for n in num_list]


def main():
    print("Testing mean function")
    n_list = [34, 44, 23, 46, 12, 24]
    correct_mean = 30.5
    assert(mean(n_list) == correct_mean)

    print("Testing add_five function")
    correct_list = [39, 49, 28, 51, 17, 29]
    assert(add_five(n_list) == correct_list)

    print("All tests passed!")

    
if __name__ == '__main__':
    main()

 

3.上述 useful_functions.py 存在哪呢?

注意,是存在第一个路径, 即【路径1】。

 

4.试一试 import !

新建 .py / .ipynb ,输入以下命令:

import useful_functions as uf
uf.add_five([1, 2, 3, 4])

 

查看结果!

 

posted on 2024-09-04 18:54  Mira_2019  阅读(4)  评论(0编辑  收藏  举报