Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pyinstaller规范文件中执行py脚本_Python_Python 3.x_Pyinstaller_Versioninfo - Fatal编程技术网

Python 在pyinstaller规范文件中执行py脚本

Python 在pyinstaller规范文件中执行py脚本,python,python-3.x,pyinstaller,versioninfo,Python,Python 3.x,Pyinstaller,Versioninfo,我使用PyInstaller将我的应用捆绑在exe中(对于windows,从现在开始)。我已通过创建version.rc文件成功地将版本信息添加到exe文件中。我还想在运行时使用当前版本来做出一些决定。因此,我希望在使用pyinstaller创建包时动态创建version.rc文件。还希望只参考当前版本。因此,我在根目录下创建了一个_version.py文件。此文件包含一个属性和一个函数 __version__ = '0.0.0.1' def write_version_file():

我使用PyInstaller将我的应用捆绑在exe中(对于windows,从现在开始)。我已通过创建version.rc文件成功地将版本信息添加到exe文件中。我还想在运行时使用当前版本来做出一些决定。因此,我希望在使用pyinstaller创建包时动态创建version.rc文件。还希望只参考当前版本。因此,我在根目录下创建了一个_version.py文件。此文件包含一个属性和一个函数

__version__ = '0.0.0.1'
def write_version_file():
    content = f'''
    VSVersionInfo(
        ffi=FixedFileInfo(
        mask=0x3f,
        flags=0x0,
        OS=0x40004,
        fileType=0x1,
        subtype=0x0,
        date=(0, 0)),
        kids=[StringFileInfo([StringTable(
        u'040904B0',
        [StringStruct(u'FileDescription', u'spagent'),
        StringStruct(u'FileVersion', u'{__verison__}'),
        StringStruct(u'InternalName', u'spagent'),
        StringStruct(u'LegalCopyright', u'Copyright'),
        StringStruct(u'OriginalFilename', u'spagent'),
        StringStruct(u'ProductName', u'spagent'),
        StringStruct(u'ProductVersion', u'{__verison__}'),
        StringStruct(u'Language', u'Language Neutral'),
        StringStruct(u'LegalTrademarks', u'')])]), 
        VarFileInfo([VarStruct(u'Translation', [1033, 1200])])]
    )
    '''
    filePath = os.path.join('version.rc')
    with open(filePath, 'w') as w:
        w.write(content)
我试图在spec文件中执行write_version_file脚本,以便它在创建build之前写入version.rc文件。并使用该文件进行版本控制。下面是我的main.spec文件的摘录

# -*- mode: python -*-
block_cipher = None
import shutil
import glob
from distutils.dir_util import copy_tree

#create version file
from _version import write_version_file
write_version_file()
我的目录结构如下

spagent
--_version.py
--main.py
--main.sepec
--other directories
pyinstaller main.spec无法执行脚本,因为它无法导入_版本模块

Traceback (most recent call last):
  File "C:\Users\chaudm05\AppData\Local\Programs\Python\Python37-32\Scripts\pyinstaller-script.py", line 11, in <module>
    load_entry_point('PyInstaller==3.5', 'console_scripts', 'pyinstaller')()
  File "c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site-packages\PyInstaller\__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site-packages\PyInstaller\building\build_main.py", line 844, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site-packages\PyInstaller\building\build_main.py", line 791, in build
    exec(code, spec_namespace)
  File "main.spec", line 8, in <module>
    from _version import write_version_file
ModuleNotFoundError: No module named '_version'
回溯(最近一次呼叫最后一次):
文件“C:\Users\chaudm05\AppData\Local\Programs\Python\Python37-32\Scripts\pyinstaller script.py”,第11行,在
加载入口点('PyInstaller==3.5','console\u scripts','PyInstaller')()
文件“c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site packages\PyInstaller\uuuuuu main\uuuuuuuuu.py”,第111行,正在运行
运行构建(pyi\u配置,规范文件,**vars(args))
文件“c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site packages\PyInstaller\uuuuuu main\uuuuuuuuuu.py”,第63行,在run\u build中
PyInstaller.building.build\u main.main(pyi\u配置,spec\u文件,**kwargs)
文件“c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site packages\PyInstaller\building\build\u main.py”,第844行,在main中
构建(specfile,kw.get('distpath'),kw.get('workpath'),kw.get('clean_build'))
文件“c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site packages\PyInstaller\building\build\u main.py”,第791行,内部版本
exec(代码、规范和命名空间)
文件“main.spec”,第8行,在
从版本导入写入版本文件
ModuleNotFoundError:没有名为“\u版本”的模块
我怀疑是这样,因为pyinstaller脚本在不同的位置执行(c:\users\chaudm05\appdata\local\programs\python\python37-32\lib\site packages\pyinstaller\building\building\u main.py),因此它找不到_version.py。如果这是真的,我该如何实现它呢?

main.spec文件中的subprocess.Popen('python version.py',shell=true)对我很有用。我必须在我的uu version.py文件中添加以下行,以便在执行子进程时可以执行所需的函数。def write_version_file():如果u name==“main”则传递:write_version_file()