[英]PermissionError: [Errno 1] Operation not permitted: '/Users/<local_path>/venv/pyvenv.cfg'
I am running a Django application with a few cron tasks.我正在运行一个带有几个 cron 任务的 Django 应用程序。 Whenever I run
python manage.py crontab add
to register the cron tasks, I get a "mail" in the Terminal showing the following traceback:每当我运行
python manage.py crontab add
来注册 cron 任务时,我都会在终端中收到一封“邮件”,显示以下回溯:
Traceback (most recent call last):
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 609, in <module>
main()
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 592, in main
known_paths = venv(known_paths)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 510, in venv
with open(virtual_conf, encoding='utf-8') as f:
PermissionError: [Errno 1] Operation not permitted: '/Users/<local_path>/venv/pyvenv.cfg'
I am running on macOS Catalina, python 3.7.3 in a virtual environment venv.我在虚拟环境 venv 中运行 macOS Catalina,python 3.7.3。
I have tried giving full disk permission to bash, crontab, terminal, xcode and pycharm.我已经尝试为 bash、crontab、终端、xcode 和 pycharm 授予完整磁盘权限。
I have also tried installing python versions 3.7.8 and 3.8.3我也尝试安装 python 版本 3.7.8 和 3.8.3
I disabled System Integrity Protection using csrutil disable
in Recovery Mode and the cron ran successfully.我在恢复模式下使用
csrutil disable
禁用了系统完整性保护,并且 cron 成功运行。 However, I do not see it as full solution.但是,我不认为它是完整的解决方案。
I dockerized the app and ran it as a docker container.我将应用程序 docker 化并作为 docker 容器运行。
I was facing the same problem and after a few searching i found a error with cron permissions.我遇到了同样的问题,经过几次搜索后,我发现 cron 权限错误。
This is how i solved:这就是我解决的方法:
Go to System Preferences -> Security & Privacy - Privacy -> Full Disk Access Go 到系统偏好设置 -> 安全和隐私 - 隐私 -> 全盘访问
Click on the + icon to add an item单击 + 图标以添加项目
If the lock appears locked, click on him to unlock with your user password如果锁显示为已锁定,请单击他以使用您的用户密码解锁
Press Command + Shift + G to open a dialog按 Command + Shift + G 打开一个对话框
Type /usr/sbin/cron and press Enter输入 /usr/sbin/cron 并按 Enter
Search and select the cron file and click Open搜索和 select cron 文件并单击打开
Okay, now it should work.好的,现在它应该可以工作了。
The reason for the PermissionError is that the Python virtual environment is placed in the user directory. PermissionError的原因是Python虚拟环境放在用户目录下。 If you put the virtual environment in a system directory, such as /opt, there will be no problem.
如果把虚拟环境放在系统目录下,比如/opt,就没有问题了。 See example below:
请参见下面的示例:
$ sudo mkdir /opt/myvenv
$ cd /opt/myvenv
$ sudo python3 -m venv .venv
$ mkdir ~/workspaces/myapp
$ cd ~/workspaces/myapp
$ cat <<EOF> demo.py
> with open('/Users/username/workspaces/myapp/demo.txt', 'a+') as f:
> f.write('Hello, cron.\n')
> EOF
$ crontab -e
$ crontab -l
* * * * * /opt/myvenv/.venv/bin/python /Users/username/workspaces/myapp/demo.py >> /Users/username/workspaces/myapp/demo.log 2>&1
$ ls -al
total 24
drwxr-xr-x 5 liqiang staff 160B 6 20 09:24 .
drwxr-xr-x 136 liqiang staff 4.3K 6 20 09:18 ..
-rw-r--r-- 1 liqiang staff 0B 6 20 09:23 demo.log
-rw-r--r-- 1 liqiang staff 117B 6 20 09:23 demo.py
-rw-r--r-- 1 liqiang staff 11B 6 20 09:24 demo.txt
This one worked for me only on Big Sur
这个只在
Big Sur
对我有用
https://www.bejarano.io/fixing-cron-jobs-in-mojave/ https://www.bejarano.io/fixing-cron-jobs-in-mojave/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.