-1

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<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

2 Answers2

0
  • 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="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  1. Then re-login, and now try to call Django with sudo.

You can also use this temporary solution:

sudo -E env "PATH=$PATH" your_command
0

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.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 21 '23 at 13:27