我创建了一个看起来像这样的 python 包:
/command
/command
module.py
__main__.py
README.md
setup.py
file.txt
Run Code Online (Sandbox Code Playgroud)
要安装我运行:
sudo python setup.py install
Run Code Online (Sandbox Code Playgroud)
现在当我打电话
$ command
Run Code Online (Sandbox Code Playgroud)
它显示此错误:
FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'
Run Code Online (Sandbox Code Playgroud)
大约有模块setup.py,__main__.py和module.py 的内容
设置文件
import setuptools
setuptools.setup(
name='command',
...
entry_points={
'console_scripts': [
'command = command.__main__:main'
]
}
)
Run Code Online (Sandbox Code Playgroud)
__main__.py
from . import module
def main():
module.run('arg')
Run Code Online (Sandbox Code Playgroud)
模块.py
import shutil
# copy file.txt from command project into the directory where it is running
def run(arg):
shutil.copyfile('../file.txt', './file.txt')
Run Code Online (Sandbox Code Playgroud)
通过以下方式安装此包后:
sudo …Run Code Online (Sandbox Code Playgroud) 所以我想在Python中使用这样的东西:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return self.name, self.age
p = Person('A', 20)
Run Code Online (Sandbox Code Playgroud)
然后希望直接调用对象p来获取元组(self.name,self.age)
但正如您在运行此程序时所看到的那样,您会遇到问题:
TypeError: __repr__ returned non-string (type tuple)
Run Code Online (Sandbox Code Playgroud)
怎么会有这种行为?
谢谢!
注意:问题不是特定于元组数据类型; 它可以是任何东西,例如像熊猫数据帧.我只想返回一些属性数据,无论它是什么类型.