gpt4 book ai didi

python - 我需要对我的 python 代码做些什么才能让它成为一个模块?

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

谁能告诉我将其打包为模块时做错了什么: https://github.com/xamox/python-zxing .

我的setup.py如下:

#!/usr/bin/env python

from distutils.core import setup

setup(
name='zxing',
version='0.1',
description="wrapper for zebra crossing (zxing) barcode library",
url='http://simplecv.org',
author='Ingenuitas',
author_email='public.relations@ingenuitas.com',
packages=['zxing'],
)

我正在尝试“导入 zxing”。我安装了 setup.py,将其放入/usr/local/lib/python2.7/dist-packages/,但导入 zxing 不起作用。

我收到以下错误:

In [1]: import zxing.zxing
---------------------------------------------------------------------------
ResolutionError Traceback (most recent call last)
/home/xamox/<ipython-input-1-9ff7d0755c55> in <module>()
----> 1 import zxing.zxing

/usr/local/bin/zxing.py in <module>()
3 __requires__ = 'zxing==0.1'
4 import pkg_resources
----> 5 pkg_resources.run_script('zxing==0.1', 'zxing.py')

/usr/lib/python2.7/dist-packages/pkg_resources.pyc in run_script(self, requires, script_name)
465 ns.clear()
466 ns['__name__'] = name
--> 467 self.require(requires)[0].run_script(script_name, ns)
468
469

/usr/lib/python2.7/dist-packages/pkg_resources.pyc in run_script(self, script_name, namespace)
1192 script = 'scripts/'+script_name
1193 if not self.has_metadata(script):
-> 1194 raise ResolutionError("No script named %r" % script_name)
1195 script_text = self.get_metadata(script).replace('\r\n','\n')
1196 script_text = script_text.replace('\r','\n')

ResolutionError: No script named 'zxing.py'

最佳答案

当你做的时候

import zxing.zxing

一切正常(dir(zxing.zxing) = ['BarCode', 'BarCodeReader', ...])。可能你希望人们只需要使用

import zxing

如果那是你想要的,你必须把下面的代码放到zxing/__init__.py

from zxing import *

或者更好

from zxing import BarCode, BarcodeReader, ...

关于python - 我需要对我的 python 代码做些什么才能让它成为一个模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9282204/

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