python-3.x - pyinstaller 更改文件路径的值
问题描述
我正在运行以下代码:
DIRECTORY_PATH = os.getcwd()
print(DIRECTORY_PATH)
print(__file__)
print(os.path.abspath(__file__))
input("Press enter to continue ...")
但是,当我使用 pyinstaller 与仅运行 python 脚本后运行时,我会得到不同的结果。有没有办法获取exe所在的目录?
运行 pyinstaller --onefile exe(注意黑色 python shell 后面的蓝色 powershell,其中包含用于运行 exe 的命令)
解决方案
我发现的唯一方法是制作一个批处理文件以从同一目录运行程序。
cd %~dp0
start program.exe
这样,即使从技术上讲,因为它来自批处理文件,您也可以获得起始文件的目录路径。
推荐阅读
- swift - How to make Custom Xib Cell Resize Correctly
- amazon-web-services - How do I write an AWS IAM policy that grants read access to everyone else's buckets?
- python - Python 中是否有 gridExtra/cowplot 类型包与 Plotnine 一起使用来对齐子图(即边际分布)
- ios - UITableview 中的文本条目类似于“任务/提醒”应用程序
- java - 将循环转换为 lambda 表达式
- cron - 如何在没有打开永久远程桌面连接的情况下运行依赖于桌面的 cron 作业?
- c - 在空指针上使用 malloc 时出现分段错误 (SIGSEGV)
- angular - 如何从 Angular 拦截器执行异步操作?
- mysql - MySQL:获取在特定日期之前但未在特定日期之后登录的所有用户,未按预期工作
- xamarin.forms - 如何以编程方式从 Xamarin.Forms ScrollView 中删除内容?