系统无法识别已安装的Python包。

huangapple go评论82阅读模式
英文:

System cannot identify installed Python packages

问题

通过pip3安装的任何包都声明为“安装成功”,但尽管将路径包含在$PATH环境变量中,但无法识别。

迄今为止,尝试了以下操作:

  • 重新安装Python
  • 重置$PATH并手动添加pip3包目录,使用pip3 list -v找到
  • 在虚拟环境中安装包。

在macOS Ventura 13.4上的项目目录antons_pizza中演示了以下问题,使用zsh shell。

然后在虚拟环境中

➜  antons_pizza source antons_pizza_venv/bin/activate
(antons_pizza_venv) ➜  antons_pizza pip3 install django
Requirement already satisfied: django in ./antons_pizza_venv/lib/python3.11/site-packages (4.2.2)
Requirement already satisfied: asgiref<4,>=3.6.0 in ./antons_pizza_venv/lib/python3.11/site-packages (from django) (3.7.2)
Requirement already satisfied: sqlparse>=0.3.1 in ./antons_pizza_venv/lib/python3.11/site-packages (from django) (0.4.4)
(antons_pizza_venv) ➜  antons_pizza django admin startproject antons_pizza     
zsh: permission denied: django
(antons_pizza_venv) ➜  antons_pizza sudo django admin startproject antons_pizza
Password:
sudo: django: command not found
英文:

Any package installed through pip3 is declared to be 'successfully installed' but cannot be identified despite the path being included in the the $PATH environment variable.

So far have attempted the following:

  • reinstalled Python
  • Reset $PATH and manually added pip3 packages directory, found using pip3 list -v
  • installing packages within a venv.

The issue is demonstrated below in the project directory antons_pizza on macOS Ventura 13.4 with zsh shell.

➜ echo $PATH                            
/usr/local/lib/python3.11/site-packages:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin

➜  antons_pizza pip3 show django                           
Name: Django
Version: 4.2.2
Summary: A high-level Python web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD-3-Clause
Location: /usr/local/lib/python3.11/site-packages
Requires: asgiref, sqlparse
Required-by: 

➜  antons_pizza django admin startproject antons_pizza     
zsh: permission denied: django

➜  antons_pizza sudo django admin startproject antons_pizza
sudo: django: command not found

Then in the venv

➜  antons_pizza source antons_pizza_venv/bin/activate
(antons_pizza_venv) ➜  antons_pizza pip3 install django
Requirement already satisfied: django in ./antons_pizza_venv/lib/python3.11/site-packages (4.2.2)
Requirement already satisfied: asgiref&lt;4,&gt;=3.6.0 in ./antons_pizza_venv/lib/python3.11/site-packages (from django) (3.7.2)
Requirement already satisfied: sqlparse&gt;=0.3.1 in ./antons_pizza_venv/lib/python3.11/site-packages (from django) (0.4.4)
(antons_pizza_venv) ➜  antons_pizza django admin startproject antons_pizza     
zsh: permission denied: django
(antons_pizza_venv) ➜  antons_pizza sudo django admin startproject antons_pizza
Password:
sudo: django: command not found

答案1

得分: 0

  • 首先确保它具有可执行权限:
ls -l /usr/local/lib/python3.11/site-packages
  • 现在,问题是当您使用sudo执行某些程序时,在执行命令之前不会加载登录shell环境,因此将使用/etc/sudoers中的默认PATH,并且我们会收到“command not found”错误。

  • 解决方法:

  1. 打开sudoers文件,取消注释secure_path变量并在其中添加您的路径。
sudo visudo
  1. 找到此行并取消注释,并在其中添加您的路径。
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  1. 然后重新登录,现在尝试使用sudo调用Django。

您还可以使用此临时解决方法:

sudo -E env "PATH=$PATH" your_command
英文:
  • First of all make sure that it has executable permissions:
ls -l /usr/local/lib/python3.11/site-packages
  • Now, the problem is when you execute some program with sudo it does not load a login shell environment before executing the command, so the default PATH from /etc/sudoers is used, and we get a command not found error.

  • Solution:

  1. open the sudoers file and add uncomment the secure_path variable and add your path in it.
sudo visudo
  1. Find this line and uncomment it and add your path to it.
Defaults secure_path=&quot;/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin&quot;
  1. Then re-login, and now try to call Django with sudo.

> You can also use this temporary solution:

sudo -E env &quot;PATH=$PATH&quot; your_command

答案2

得分: 0

由@FlyingTeller在评论中回答,遗憾的是这只是一个简单的拼写错误。

我试图运行django admin,而实际命令应该是django-admin

英文:

Answered in the comments by @FlyingTeller, alas it was a simple typo.

I was trying to run django admin when the command is in fact django-admin.

huangapple
  • 本文由 发表于 2023年6月19日 19:00:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定