简体   繁体   English

未找到命令:django-admin 在 mac 中使用 django-admin startproject prj 时

[英]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 使用命令启动一个新项目

django-admin startproject prj1

in Mac, but it shows command not found:django-admin.在 Mac 中,但它显示未找到命令:django-admin。

The --user option conveniently installs packages without requiring admin access, in a place where Python will also look when importing modules. --user选项可以方便地安装包,而无需管理员访问权限,Python 在导入模块时也会查看该位置。 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).在 MacOS 上,安装位置在/Users/<username>/Library/Python/<xy>/lib/python/site-packages/ (其中<xy>表示 Python 版本,例如 2.7,仍然是默认的) MacOS 上的 Python 版本)。 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 .在任何情况下,用户都不必理会这些信息,因为如前所述,会自动搜索导入位置:无需将此位置添加到环境变量 (envvar) PYTHONPATH

Perhaps somewhat unfortunately, this is not the case for executables that are installed together with packages, such as django-admin .也许有些遗憾,对于与软件包一起安装的可执行文件(例如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.它只是 Python 以及其他一些类型的软件所使用的东西。 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.因此,必须手动将相应的路径添加到环境变量PATH ,该环境变量定义了在哪些位置搜索可执行文件。 The path where executables are installed is slightly different than the above path: /Users/<username>/Library/Python/<xy>/bin/ .安装可执行文件的路径与上述路径略有不同: /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).因此,必须设置PATH envvar 以添加(大多数情况下,添加它的前面),并使其更持久,“导出”变量(在 bash 或 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.为了使这更持久,以便在每个人启动一个新终端时都能正常工作,请将上述内容添加到/Users/<username>/.bashrc文件的最后。 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.这需要使用-m选项运行python ,后跟包名称,在这种情况下,再后跟相关命令和参数。

Thus the following also (always) works, without having to extend PATH as above:因此,以下也(总是)有效,而不必像上面那样扩展PATH

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 ).如果你在一个尴尬的位置安装一个新的 Python 版本(例如 /opt/local 中的 Python 3.7 或其他东西),并使用别名而不是设置PATH (我经常使用alias py37=/opt/local/bin/python3.7 ),后者也很方便alias py37=/opt/local/bin/python3.7 )。 And then use python3.7 -m <package> ... as above.然后使用python3.7 -m <package> ...如上所述。 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.然而,这正是人们所拥有的偏好:其他人只是希望能够简单地输入django-admin并完成它。

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...然后立即运行创建项目命令。注意现在您将在 your_env 上方创建的 venv 中...

......django-admin startproject prji

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

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