Can't open file 'django-admin.py': [Er

2020-02-05 03:17发布

I'm using Python 2.7, Django 1.2.5 and on Windows 7.

I am not sure what I've done. I used to be able to create Django projects like

python django-admin.py startproject test

Now however I get this error.

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

I can type the following which works.

python C:\Python27\Scripts\django-admin.py startproject test

How can I have it the way it used to be? Not having the type the full path to the django-admin.py file.

Things I've already tried:

I uninstalled Python and manually removed the values from the PATH variable in Windows. Reinstalled Python. Deleted Django and reinstalled it too.

I've added C:\Python27\Scripts to my PATH and PYTHONPATH variable under Environmental Variables in Windows.

Any suggestions?

My PATH variable contains

C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\ActiveState Komodo Edit 6\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\django-apps;F:\My_Projects;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Mercurial\bin;C:\Python27;C:\Python27\Scripts

PYTHONPATH has

C:\Python27;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts;C:\django-apps;f:\my_projects

12条回答
对你真心纯属浪费
2楼-- · 2020-02-05 03:48

First, set the path in PowerShell (in your $profile) like this:

$env:PATH = "C:\Python27\;C:\Python27\Scripts;c:\python27\lib\site-packages\django\bin\;"

Then, to get .py files to open in PowerShell rather than cmd, add this line:

$env:PATHEXT += ";.py"

Finally, to be able to just type "django-admin" and have it work, add this line:

function django-admin {python (gcm django-admin.py | resolve-path) $args}

That should do the trick.

查看更多
够拽才男人
3楼-- · 2020-02-05 03:52

The problem seems to be with the file association. After adding the PATH variables, remove all "py" associations instead of linking them to your Python executable:

Remove the file type ".py " from the registry or by using a small free tool for Windows Vista/Windows 7 called 'Unassoc' (google for Windows 7 unassoc).

In my case simply unassociating the file type was not enough. I had to remove the file type entirely using the unassoc tool.

Now you should be good to go.

查看更多
混吃等死
4楼-- · 2020-02-05 03:52

On Windows, I used

django-admin startproject test 

and it seemed to have worked.

查看更多
孤傲高冷的网名
5楼-- · 2020-02-05 03:52

Make sure that you cd is where you saved your Notepad++ Python file. If you saved it under your \Python27\ directory, then while in the terminal or PowerShell window, type cd C:\Python27 and press enter.

This will open that directory so when you type python filename.py it will find it and run it. Just remember to save all .py files to the same directory, so you have no further problems.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-02-05 03:53

Is it possible you associated your .py files with another program (like a text editor)? I had this problem, too, after associating .py files with gedit.

I'd have the problem if I did this:

python django-admin.py startproject myproject

Once I reassociated .py files to python this problem went away.

查看更多
等我变得足够好
7楼-- · 2020-02-05 03:54

Here is what I did to get this working:

  • Installed Python 2.7 to C:\Python27 using the install package for Windows at python.org
  • I chose to install the latest release version of Django (1.3) (not the development trunk)
  • Installed Django following the instructions here: http://docs.djangoproject.com/en/dev/topics/install/
  • I'm only doing local development, so I skipped install of Apache and mod_wsgi (as directed in Django instructions)
  • I don't need a database for my app, so I skipped "Get your database running" section in Django instructions; and I skipped "Remove any old versions of Django" (didn't have any installed)
  • I skipped down the instructions to "Installing an official release"
  • I installed bsdtar as directed in order to untar the release files on my windows machine - ran no problem.
  • Being on Windows, I started a cmd shell with admin privileges and ran the command "setup.py install" - ran no problem
  • I followed the instruction to verify Django install: running import django and django.print get_version() - returned 1.3, success
  • Then it came time to run django-admin.py startproject myproject - I received similar errors to the above, and when trying to run django-admin.py from within the python interpreter, I received syntax errors pointing at the arguments. Strange.
  • Found this thread, and ran this, which worked: c:\Python27\Scripts\django-admin.py startproject myproject and it worked.
  • Still curious, I wanted to see if I could make it work from cmd prompt, without the paths, since it seemed that should work - and it didn't.

This is what I did beyond the install instructions that made it work for me on Windows:

  • Verified the file associations, using info from this post - all associations good.
  • Used Start|Computer|System Properties|Advanced System Properties|Environment Variables dialog to set the environment variables as follows:
  • Set New System Variable: PYTHONPATH = c:\Python27\Lib;c:\Python27\Scripts
  • Edit existing User variable: PATH added: C:\Python27;C:\Python27\Scripts to the end.

The result:

  • .py files now execute from cmd command line (no need to run python interpreter first)
  • e.g. the command django-admin.py startproject mynewproject ran just fine.

Please post any questions in the comments, maybe I can help.

查看更多
登录 后发表回答