简体   繁体   English

WindowsError:[错误 5] Flask 中的访问被拒绝

[英]WindowsError: [Error 5] Access is denied in Flask

I am trying to move an uploaded file to a specific folder in my Windows system and it gives me WindowsError: [Error 5] Access is denied error.我正在尝试将上传的文件移动到我的 Windows 系统中的特定文件夹,它给了我 WindowsError: [Error 5] Access is denied 错误。 The solutions I happen to see for such problems are run python as Administrator from cmd line.对于此类问题,我碰巧看到的解决方案是以 cmd 行的管理员身份运行 python 。 I am not sure if that is possible since it's a web app and i am using the default flask server for development purpose and run it from Pycharm.我不确定这是否可能,因为它是 web 应用程序,并且我使用默认的 flask 服务器进行开发,并从 ZE47D298C46B582C0F8D0F6930664F051 运行它。

my code is我的代码是

@app.route('/test',methods=['POST'])
def test():
    import os
    if not os.path.exists("history_plugin"):
        os.makedirs("test")
        f = open('test/abc.txt', 'w+')
        f.close()

Running the application 'directly in pycharm' is the equivlant of running it on the command prompt, but with a few caveats. “直接在 pycharm 中”运行应用程序相当于在命令提示符下运行它,但有一些注意事项。 Personally I don't like running python in pycharm as I find it can cause errors.我个人不喜欢在 pycharm 中运行 python,因为我发现它会导致错误。

Ideally you don't want to run as administrator, but you might find you have a couple of problems when it comes to windows.理想情况下,您不想以管理员身份运行,但您可能会发现在 Windows 方面存在一些问题。 Firstly are you sure the Access Denied is from the file, and not from the trying to bind the app to port 80 (also note other issues with trying to bind on windows such as Skype taking over port 80)首先,您确定拒绝访问来自文件,而不是尝试将应用程序绑定到端口 80(还要注意尝试在 Windows 上绑定的其他问题,例如 Skype 接管端口 80)

If the problem is caused by the mkdir, make sure your user has permissions on the parent folder, not just the folder its creating.如果问题是由 mkdir 引起的,请确保您的用户对父文件夹具有权限,而不仅仅是其创建的文件夹。 You are right to be wary of running as admin.您对以管理员身份运行保持警惕是正确的。 Generally speaking you should create users per service and run as that, but it can be a pain during development (also, in 'production' you would want to run something like uwsgi or similar to act as a python process manager).一般来说,您应该为每个服务创建用户并以此方式运行,但这在开发过程中可能会很痛苦(此外,在“生产”中,您可能希望运行类似 uwsgi 或类似的东西来充当 python 进程管理器)。

The other thing to note is that where you are running from - if you're running from your Desktop folder, I have noticed that this can also have strange permission problems for applications - but I'm assuming you're in a user 'workbench' folder of some kind.另一件要注意的事情是,你从哪里运行——如果你从你的桌面文件夹运行,我注意到这也可能对应用程序有奇怪的权限问题——但我假设你在用户的工作台中' 某种文件夹。

I had been running the application directly from Pycharm, which doesn't run it in administrator mode我一直在直接从 Pycharm 运行该应用程序,它不会在管理员模式下运行它

I tried running it using command prompt as administrator and it worked for me.我尝试以管理员身份使用命令提示符运行它,它对我有用。

if running on windows and inside a python virtual environment "venv "如果在 windows 和 python 虚拟环境“venv”内运行

type pip install flask类型pip install flask

should do the trick, you must've been using a global flask installation应该可以解决问题,您一定一直在使用全局 flask 安装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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