我有一个 python 类,它有几个属性。我想实现一个方法,它将返回一些属性作为字典。我想用装饰器标记属性。这是一个例子:
class Foo:
@export_to_dict # I want to add this property to dict
@property
def bar1(self):
return 1
@property # I don't want to add this propetry to dict
def bar2(self):
return {"smth": 2}
@export_to_dict # I want to add this property to dict
@property
def bar3(self):
return "a"
@property
def bar4(self):
return [2, 3, 4]
def to_dict(self):
return ... # expected result: {"bar1": 1, "bar3": "a"}
Run Code Online (Sandbox Code Playgroud)
实现它的一种方法是使用export_to_dict装饰器为属性设置一个附加属性,如下所示:
def export_to_dict(func):
setattr(func, '_export_to_dict', True)
return func
Run Code Online (Sandbox Code Playgroud)
并通过调用_export_to_dict时的属性来搜索属性。 …
我有一个 Python 应用程序,我最近在其中添加了一个 Cython 模块。使用 pyximport 从脚本运行它工作正常,但我还需要一个我用 cx_Freeze 构建的可执行版本。
问题是,尝试构建它给了我一个可执行文件,该可执行文件在尝试导入 .pyx 模块时引发了 ImportError。
我修改了我的setup.py喜欢,看看我是否可以先编译 .pyx 以便 cx_Freeze 可以成功打包它:
from cx_Freeze import setup, Executable
from Cython.Build import cythonize
setup(name='projectname',
version='0.0',
description=' ',
options={"build_exe": {"packages":["pygame","fx"]},'build_ext': {'compiler': 'mingw32'}},
ext_modules=cythonize("fx.pyx"),
executables=[Executable('main.py',targetName="myproject.exe",base = "Win32GUI")],
requires=['pygcurse','pyperclip','rsa','dill','numpy']
)
Run Code Online (Sandbox Code Playgroud)
...但是所有给我的都是No module named fx在构建时的 cx_Freeze 中。
我如何使这项工作?
我正在尝试使用cx_Freeze编译我的python脚本,这是我的安装文件:
import cx_Freeze
import sys
import matplotlib
import os
base = None
if sys.platform == 'win32':
base = "Win32GUI"
os.environ['TCL_LIBRARY'] = r'C:\\Python35\\tcl\\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\\Python35\\tcl\\tk8.6'
executables = [cx_Freeze.Executable("HomeScreen.py", base=base,
icon="icon.png")]
cx_Freeze.setup(
name = "LeagueBoost",
options = {"build_exe":{"packages": ["sqlite3","requests","time","sys","os","statistics","matplotlib","random","collections"],
"include_files": ["Assets", "LeagueBoost_v1.py","LBRun.py","graphSetup.py","profilepage.py","Assets_rc.py"]}},
version = "1",
executables = executables
)
Run Code Online (Sandbox Code Playgroud)
但是,当我给出cmd命令时C:/python35/python.exe,它会copying C:\python35\python35.dll -> build\exe.win-amd64-3.5\python35.dll弹出“ python停止工作”
我正在尝试从 python 脚本(使用 PyQt4 GUI 和 matplotlib)创建一个 .exe 文件。我将 cx_Freeze 5.1.1 版用于 64 位 Windows,并具有以下功能setup.py:
import cx_Freeze
import sys
import matplotlib
base = "Win32GUI"
includes = ["atexit"]
buildOptions = dict(
#create_shared_zip=False,
#append_script_to_exe=True,
includes=includes
)
executables = [cx_Freeze.Executable(script = "main.py", base = base)] # icon = "chart32.jpg")]
cx_Freeze.setup(
name= "1ChPlotGUI",
options = dict(build_exe=buildOptions), # {"build_exe": {"packages": ["matplotlib"], "include_files":["chart32.jpg"]}},
version = "0.01",
description = "1 Channel Plotting app with GUI",
executables = executables
)
Run Code Online (Sandbox Code Playgroud)
跑完后
python setup.py build
Run Code Online (Sandbox Code Playgroud)
在 cmd …