简体   繁体   中英

command not found: django-admin when using django-admin startproject prj in mac

I'm trying to start a new project using django usign the command

django-admin startproject prj1

in Mac, but it shows command not found:django-admin.

The --user option conveniently installs packages without requiring admin access, in a place where Python will also look when importing modules. On MacOS, the installation location is in /Users/<username>/Library/Python/<xy>/lib/python/site-packages/ (with <xy> indicating the Python version, such as 2.7, which is still the default Python version on MacOS). For other OSes, this will be a different location. In any case, users do not have to bother with this information, because the import location is, as mentioned, automatically searched: there is no need to add this location to the environment variable (envvar) PYTHONPATH .

Perhaps somewhat unfortunately, this is not the case for executables that are installed together with packages, such as django-admin . The reason is that this "user-installed package & executable" is not standardized across all types of software; it's just something that Python, and perhaps a few other types of software, use. Therefore, one manually has to add the corresponding path to the environment variable PATH , which is the envvar that defines in which locations executables are searched for. The path where executables are installed is slightly different than the above path: /Users/<username>/Library/Python/<xy>/bin/ .

Thus, one has to set the PATH envvar to add (most of the time, add the front of it), and to make it more permanent, "export" the variable (in bash or zsh shell). Thus:

export PATH=/Users/<username>/Library/Python/<x.y>/bin/:$PATH

To make this more permanent, so that it works each one starts up a new terminal, add the above to your /Users/<username>/.bashrc file, at the end. Then, this is set each time you open a new terminal.


Finally, many packages that also install executables also have the option to run as an executable module. This requires running python with the -m option, followed by the package name, and in this case, followed again by the relevant command and argument.

Thus the following also (always) works, without having to extend PATH as above:

python -m django startproject prj1

The latter is also convenient if you install a new Python version in an awkward location (eg, Python 3.7 in /opt/local or something), and use an alias instead of setting PATH (I often use eg alias py37=/opt/local/bin/python3.7 ). And then use python3.7 -m <package> ... as above. This, however, is just what preferences people have: other people just want to be able to simply type django-admin and be done with it.

First Activate the virtual environment ..type in

......source your_env/bin/activate

Then immediatly run the create project command.Note now you will be in the venv created above your_env...

......django-admin startproject prji

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