首页 > 解决方案 > 如何在可执行文件中创建 python 文件并使用它?

问题描述

我想在可执行文件(pyinstaller)中创建python文件并使用它。该模块必须绝对隐藏,否则可能导致安全故障。

我测试过:

PATH = os.path.dirname(os.path.dirname(__file__))
sys.path.append(os.path.dirname(sys.executable))
pathm = f'{PATH}\\module'
sys.path.append(os.path.dirname(sys.executable))
sys.path.append(os.path.dirname(PATH))
pathModule = f'{PATH}\\module\\file.py'
sys.path.append(os.path.dirname(pathModule))

但它不起作用。我更新了所有模块。

控制台输出:

Ready for testing...
level 1
Traceback (most recent call last):
  File "testy.py", line 24, in <module>
  File "testy.py", line 15, in creatFile
FileNotFoundError: [Errno 2] No such file or directory: 'module/file.py'
[13552] Failed to execute script testy

编码:

import os
import os.path
import sys

PATH = os.path.dirname(os.path.dirname(__file__))

pathm = f'{PATH}\\module'

sys.path.append(os.path.dirname(sys.executable))

sys.path.append(os.path.dirname(PATH))

def creatFile():
    print('level 1')
    f = open('module/file.py','w')
    pathModule = f'{PATH}\\module\\file.py'
    sys.path.append(os.path.dirname(pathModule))
    f.write('#Hello world: :)\n')
    f.write('def hello():\n')
    f.write('\treturn "hello Alex"\n')
    f.close()

x = input('Ready for testing...')
creatFile()

try:
    import module.file as file
    print(file.hello())
    print('level 2')
except:
    print('ERROR')

x = input('....')

窗户 10

蟒蛇 3.9

标签: python-3.xpyinstaller

解决方案


推荐阅读