python:无法打开文件“django-admin.py”:[Errno 2] 没有那个文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23915661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:40:01  来源:igfitidea点击:

python: can't open file 'django-admin.py': [Errno 2] No such file or directory

pythondjango

提问by user2548635

I have a mac and I am starting to work on django. When I try to make a project on the terminal by writing

我有一台 mac,我开始在 django 上工作。当我尝试通过写作在终端上制作一个项目时

python django-admin.py startproject myproject

I get this error

我收到这个错误

python: can't open file 'django-admin.py': [Errno 2] No such file or directory

While I was looking around for help, one solution suggested to write type django-admin.py to get the location of django-admin.py and use that.

当我四处寻求帮助时,一种解决方案建议编写类型 django-admin.py 以获取 django-admin.py 的位置并使用它。

So when I type

所以当我输入

python /usr/local/bin/django-admin.py startproject myproject

my project is created.

我的项目已创建。

Can anyone tell my why I need to do this and why I can't write just django-admin.py? Also is there anyway to get around this?

谁能告诉我为什么我需要这样做以及为什么我不能只写 django-admin.py?还有无论如何解决这个问题吗?

采纳答案by Daniel Roseman

You're confusing two ways of referring to an executable file.

您混淆了引用可执行文件的两种方式。

/usr/local/binis in your path, and django-admin.pyis marked as executable, so you can refer to it without the initial python:

/usr/local/bin位于您的路径中,并被django-admin.py标记为可执行文件,因此您可以在没有首字母的情况下引用它python

django-admin.py startproject myproject

When you start with python, that is saying "start Python with the script at this path". So, you need to pass the full path, if the script you're trying to start isn't in your current directory.

当您以 开头时python,就是说“使用此路径上的脚本启动 Python”。因此,如果您尝试启动的脚本不在当前目录中,则需要传递完整路径。

回答by falsetru

python django-admin.py- Python execute file django-admin.pyin the current working directory.

python django-admin.py- Python的执行文件django-admin.py当前工作目录

If you add /usr/local/bininto the PATHenvironment variable, you can just issue django-admin.pyinstead of python /usr/local/bin/django-admin.py.

如果添加/usr/local/binPATH环境变量中,则可以只发出django-admin.py而不是python /usr/local/bin/django-admin.py.

  1. Check whether PATH contains /usr/local/bin

    echo $PATH
    
  2. If there's no /usr/local/binin the variable, add that:

    export PATH=$PATH:/usr/local/bin  # sh, ksh, bash, ..
    
    set path = ($path /usr/local/bin) # csh
    
  1. 检查PATH是否包含 /usr/local/bin

    echo $PATH
    
  2. 如果/usr/local/bin变量中没有,请添加:

    export PATH=$PATH:/usr/local/bin  # sh, ksh, bash, ..
    
    set path = ($path /usr/local/bin) # csh
    

回答by Padraic Cunningham

Use django-admin.py startprojectwithout the python.

django-admin.py startproject没有 python 的情况下使用。

You don't need to use python with the django-admin.py startproject, it should work from any directory. Only on windows you need to specify the full path.

您不需要将 python 与 一起使用django-admin.py startproject,它应该可以在任何目录中使用。仅在 Windows 上您需要指定完整路径。

djangoruns the adminscript from the python interpreterin your path.

django运行admin该脚本python interpreter在你的path

回答by Guru

Go to /usr/local/bin and change the permission of 'django-admin.py' as non executable then everything should work fine.

转到 /usr/local/bin 并将“django-admin.py”的权限更改为不可执行,然后一切正常。

回答by Infinity

I had the same issue, I did check my /usr/local/bin but django-admin was missing instead it was in /usr/bin.

我遇到了同样的问题,我确实检查了我的 /usr/local/bin 但缺少 django-admin 而它在 /usr/bin 中。

Later I tried below commands by executing the command in the workspace directory and it created projected for me.

后来我通过在工作区目录中执行命令来尝试下面的命令,它为我创建了投影。

python /usr/bin/django-admin startproject projectname

python /usr/bin/django-admin startproject 项目名

/usr/bin/django-admin startproject projectname

/usr/bin/django-admin startproject 项目名

回答by Elder Geek

I had a similar issue with a different python script. Adding the shebang#!/usr/bin/env pythonto the top of the script and having it my path did the trick and allowed me to call it directly as in script.pyrather than python full/path/2script/script.py

我在使用不同的 python 脚本时遇到了类似的问题。将shebang添加#!/usr/bin/env python到脚本的顶部并将其设置为我的路径即可解决问题,并允许我直接调用它script.py而不是python full/path/2script/script.py

回答by Kanna

Problem:

问题:

python: can't open file 'django-admin.py': [Errno 2] No such file or directory

python:无法打开文件“django-admin.py”:[Errno 2] 没有那个文件或目录

Cause:We get this error when we write python code in notepad & saving it as .py extension. But command line will treats it as .py.txt

原因:当我们在记事本中编写 python 代码并将其保存为 .py 扩展名时,我们会收到此错误。但是命令行会将其视为.py.txt

Solution:Just type python and drag the above file into the cmd prompt & then press enter.

解决方案:只需输入python并将上述文件拖到cmd提示符中,然后按回车。

Then sure u get it.

然后确定你明白了。

回答by Koji D'infinte

For windows users, just do django-admin startproject myprojectto use the django-admin.exe tool.

windows用户django-admin startproject myproject使用django-admin.exe工具即可。

回答by pc expert

Try this django-admin.py startproject youproname

试试这个 django-admin.py startproject youproname

i also got the same problem when i installed django in my ubuntu just use the above one it will definately work

当我在我的 ubuntu 中安装 django 时,我也遇到了同样的问题,只需使用上面的一个它肯定会工作