gpt4 book ai didi

python - 在 Python 中从自定义包导入失败

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:49 26 4
gpt4 key购买 nike

所以我在 /home/richard/projects/hello-python 目录中有一个 main.py 文件:

import sys
sys.path.append('/home/richard/projects/hello-python')

from Encode import Ffmpeg
x = Ffmpeg()
x.encode()

然后我在 /home/richard/projects/hello-python/Encode 目录中创建了一个包:

__init__.py
Ffmpeg.py

初始化文件为空。 Ffmpeg.py 文件包含:

class Ffmpeg(object):


i = 150

def __init__(self):
print "i am constructor"

def encode(self):
print "hello world"

现在我像这样运行 main.py 脚本:

python main.py

我得到这个输出:

richard@richard-desktop:~/projects/hello-python$ python main.py 
Traceback (most recent call last):
File "main.py", line 5, in <module>
x = Ffmpeg()
TypeError: 'module' object is not callable
richard@richard-desktop:~/projects/hello-python$

我认为我的 sys.path 有问题,所以我的模块无法正确导入,但我不确定如何修复它。

最佳答案

from Encode.Ffmpeg import Ffmpeg

关于python - 在 Python 中从自定义包导入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11211270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com