python-3.x - 如何在可执行文件中创建 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
解决方案
推荐阅读
- c - c语言中atoi编译错误的问题
- typescript - 添加新属性时,映射类型出现 ts(1170) 错误
- javascript - NextJs + Nodemailer + Fetch。如何接收回复
- ibm-cloud - Node-RED 显示已连接,但在 IoT 平台上显示已断开连接
- cmake - CMake 错误:安装 Trilinos 时出现未知参数 -VV
- heroku - 部署到 Heroku 时未应用按钮样式
- reactjs - useSelector() 不会在更新时重新渲染组件
- python - 在 Python 中使用 Prince 进行多重对应分析 - 无法让库运行
- java - 使用 Angular 将表单对象传递给 http delete 方法
- for-loop - 如何对具有二项式协变量数据的模型进行循环线性回归?