简体   繁体   中英

Python: Permission denied when writing to file

I am writing a script on my Windows VM to "compile" my Python scripts as a .exe file. I followed several tutorials online to no prevail. Some help from the community would be great!

Location of files: C:\\Users\\NAME\\Desktop Python Install Directory: C:\\Program Files (x86)\\Python

hello.py

print "Hello, World!"

compiler.py

from distutils.core import setup
import py2exe # has been installed
import os

filename = raw_input("File to compile: ")
setup(console=[filename])

When I run the script in CMD using the command "python compiler.py install", I get the following output:

C:\Users\NAME\Desktop>python compiler.py install
File to compile: hello.py
running install
running build
running install_egg_info
Writing C:\Program Files (x86)\Python\Lib\site-packages\UNKNOWN-0.0.0-py2.7.egg-info
error: C:\Program Files (x86)\Python\Lib\site-packages\UNKNOWN-0.0.0-py2.7.egg-info: Permission denied
C:\Users\NAME\Desktop>

I have no clue why I am receiving this error, as Python should have permission to read and write files in its installation folder. Thanks for any advice!

EDIT: I am running 2.7, I am not sure whether it matters. But just in case it does!

Python should have permission to read and write files in its installation folder

That's not actually true. Permissions are resolved on Windows not by the program that is running, but the user account which is doing the action. So the answer is that your user account does not have access to write to the Python installation folder.

In general, to install system-wide software (which you're trying to do), you would need to run your commands under a local administrator account. However, a better option might be to find a way to install your program somewhere else (for testing purposes).

Nothing to do with the permissions, it's because you're running the wrong command.

you typed in: C:\\Users\\NAME\\Desktop>python compiler.py install

which SHOULD BE:

C:\\Users\\NAME\\Desktop>python compiler.py py2exe

:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM