通过pyinstaller打包后exe文件设置代码def add_to_startup(): lujing = os.getcwd() lujing = lujing.replace('\\', '/') file_path = os.path.abspath(f'{lujing}/首检工单超时预警.exe') key = winreg.HKEY_CURRENT_USER key_value = "Software\Microsoft\Windows\CurrentVersion\Run" with winreg.OpenKey(key, key_value, 0, winreg.KEY_WRITE) as key_handle: winreg.SetValueEx(key_handle, "My Program", 0, winreg.REG_SZ, file_path)开机启动无法找到同目录excel文件
时间: 2024-03-22 07:39:35 浏览: 139
这个问题可能是因为在设置开机启动时,程序还没有执行到读取Excel文件的代码,所以找不到文件。你可以尝试在程序执行读取Excel文件的代码之前,先切换到Excel文件所在的目录,再执行读取Excel文件的代码。修改代码如下:
```
import os
import winreg
def add_to_startup():
lujing = os.getcwd()
lujing = lujing.replace('\\', '/')
file_path = os.path.abspath(f'{lujing}/首检工单超时预警.exe')
key = winreg.HKEY_CURRENT_USER
key_value = "Software\Microsoft\Windows\CurrentVersion\Run"
with winreg.OpenKey(key, key_value, 0, winreg.KEY_WRITE) as key_handle:
winreg.SetValueEx(key_handle, "My Program", 0, winreg.REG_SZ, file_path)
# 切换到Excel文件所在的目录
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# 执行读取Excel文件的代码
# ...
```
这样应该就可以解决找不到同目录Excel文件的问题了。
相关问题
将以下代码:import os import pandas as pd folder_path = "D:/活动生成" file_name = "活动.xlsx" new_file_name = "一口价3.xlsx" activity_file_path = os.path.join(folder_path, file_name) activity_df = pd.read_excel(activity_file_path) discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39} new_price_df = pd.DataFrame(columns=["商品编码", "价格"]) for index, row in activity_df.iterrows(): if row["活动"] in discounts: standard_price = row["标准售价"] discount = discounts.get(row["活动"], 1) new_price = round(standard_price * discount, 2) # convert product code to 7 digits product_code = str(row["商品编码"]).zfill(7) new_price_df.loc[index] = [product_code, new_price] new_file_path = os.path.join(folder_path, new_file_name) new_price_df.to_excel(new_file_path, index=False) print("已生成") 生成一个exe文件,并可以让我下载使用
为了将Python代码转换为可执行文件(exe文件),你可以使用PyInstaller库。首先,你需要在命令行中安装PyInstaller库:
```
pip install pyinstaller
```
安装完成后,打开命令行,进入代码所在目录,运行以下命令:
```
pyinstaller --onefile filename.py
```
其中,`filename.py`是你要打包成可执行文件的Python文件名。执行完这个命令后,PyInstaller将会在同一目录下生成一个`dist`文件夹,里面就是可执行文件。你可以将`dist`文件夹中的`filename.exe`文件发送给用户使用。
需要注意的是,如果你的Python文件中用到了第三方库,那么在生成可执行文件之前,你需要先将这些库打包进来。你可以使用以下命令生成一个包含所有依赖库的可执行文件:
```
pyinstaller --onefile --add-data "path/to/library;." filename.py
```
其中,`path/to/library`是你要打包进来的依赖库的路径。如果有多个依赖库,可以多次使用`--add-data`参数。
def hi(): print('hhhh') if __name__ == 'main': while True: str = sys.argv[1] str = sys.argv[2] hi() print('test:', str) time.sleep(10000) 我在exe如何执行
在 Windows 系统上,可以使用 pyinstaller 工具将 Python 脚本打包成可执行文件。安装完 pyinstaller 后,在命令行中切换到你的 Python 脚本所在的目录,然后运行以下命令:
```
pyinstaller --onefile your_script_name.py
```
上述命令将会在同级目录下生成一个名为 "dist" 的文件夹,里面包含了可执行文件。你可以在命令行中切换到 "dist" 目录下,然后直接运行可执行文件,如:
```
your_script_name.exe arg1 arg2
```
其中 "arg1" 和 "arg2" 分别是你要传递给脚本的两个参数。注意,在 Windows 系统上,命令行参数使用空格分隔。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)