小编use*_*473的帖子

Python cx_Freeze名称__file__未定义

我有一个python脚本,它从互联网上获取图像,下载它,设置为桌面背景并在一分钟后更新.问题很可能是cx_Freeze不包括os模块,因为具有绝对路径的相同代码工作正常.我的代码也很完美,直到它冻结.它在我通过控制台加载,从IDLE运行或双击它之前冻结.每当我运行冻结文件时,我都会收到错误(如果我使用setup.py或cxfreeze file.py:

C:\Python33\Scripts>C:\Python33\Scripts\dist\desktopchanger.exe
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\cx_Freeze\initscripts\Console3.py", line 2
7, in <module>
    exec(code, m.__dict__)
  File "C:\Python33\desktopchanger.pyw", line 7, in <module>
    dir = path.dirname(__file__)
NameError: name '__file__' is not defined
Run Code Online (Sandbox Code Playgroud)

我的守则

import pythoncom
from urllib import request
from win32com.shell import shell, shellcon
from time import sleep
from os import path

dir = path.dirname(__file__) #get dierctory script is in
startpath = str(path.join(dir+'/bg/bg.jpg')) #add /bg/bg.jpg to path of script

pathtoimg=[]
for char in startpath:
    if char != "/": …
Run Code Online (Sandbox Code Playgroud)

python python-module setup.py cx-freeze self-reference

8
推荐指数
1
解决办法
6032
查看次数