我正在完成一个项目,我需要将函数中使用的
requirements.txt
、 helper_functions.py
和 libraries.py
- 库分成 (3) 个单独的文件以进行代码美化。
我创建了 3 个文件,将它们上传到我的云端,并让我的 google Colab 笔记本可以访问它们:
我下载了
requirements.txt
并安装了软件包
当我使用命令 !pip install -r requirements.txt
运行 jupyter 笔记本时
我下载了
helper_functions
并将它们导入到我的
使用命令 from helper_functions import *
的环境,其中
我可以看到他们。
这就是现在的问题,我下载了libraries.py并尝试 使用命令
from libraries import *
或
%run -i libraries.py
。我遇到的问题是当我尝试打电话时
任何需要其中一个库的函数,例如 boto3
库或 np 的快捷方式(尽管它们都存在于我的
libraries.py 文件),我得到一个错误
name 'boto3' is not defined
name 'np' is not defined
这是加载到我的 Colab 环境中的文件的快照
`requrinments.txt`
boto3
numpy
`libraries.py file`
import boto3
import numpy as np
`helper_functions.py file`
def load_data(input):
data = np.(…..)
我该怎么办?
Python 模块被导入到给定的命名空间中。要使用模块的模块,您可以通过 module.submodule 访问它。然而,这样做很糟糕,你应该总是将它们直接导入到你的模块中,因为几乎所有时候它们都是相同的。
import mymod
np = mymod.numpy
对于所有非 hacky 代码,以上内容应类似于以下内容。
import numpy as np