开发者

Problem running compiled Python script [duplicate]

开发者 https://www.devze.com 2023-01-25 19:37 出处:网络
This question already has answers here: how can i get the executable's current directory in py2exe?
This question already has answers here: how can i get the executable's current directory in py2exe? (4 answers) Closed 8 years ago.

So I have compiled a python script with py2exe according to this answer. There were no errors during compilation, everything went fin开发者_JAVA技巧e.

When I run the script from a cmd like this:

C:\Users\Richard\Dist\backprop3.exe 60

This is the output I get:

C:\Users\Richard>C:\Users\Richard\Dist\backprop3.exe 60
Traceback (most recent call last):
  File "backprop3.py", line 209, in <module>
  File "backprop3.py", line 175, in demo
NameError: global name '__file__' is not defined

C:\Users\Richard>

Which is referring to this line:

image = Image.open( os.path.dirname( os.path.abspath( __file__ ) )+"/backprop-input.bmp" )

That line just loads an image from a current directory. Where is the problem?


__file__ will not work within py2exe. This is because the module is inside the .exe and thus there is nothing to set __file__ to which will give you the python file.

See http://www.py2exe.org/index.cgi/WhereAmI for techniques of dealing with this.

0

精彩评论

暂无评论...
验证码 换一张
取 消