15👍
I had this same problem with slightly newer versions of Python 2.7.x and Django – and it is not the PATH. This is all I had to do to fix it in Windows XP:
- Find a .py file (any, even a blank).
- Right click on it and choose: “Open with>” and then select “Choose program…”.
- This pops up a list of all programs – select
python
, and check the box “Always use the selected program to open this kind of file” and then click OK.
Checking this box resets file associations and fixes this problem for the command line.
The cause of the problem: Telling Windows to open up .py files in a text editor as default.
10👍
If C:\Python27\Scripts
is in your Path, just type in:
django-admin.py startproject proj
There should be a file association with .py and try to execute. If you prefix with python
the next command is a file path.
I’ve never been able to do python django-admin.py
— I get the same error you describe.
Make sure python is associated with .py
. You can check via file properties (opens with…) or typing assoc .py
in cmd.
- How can I schedule a Task to execute at a specific time using celery?
- In django, how to limit choices of a foreignfield based on another field in the same model?
- Python Error: name 'admin' is not defined
- How to delete nested session data in Django?
5👍
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.
- How to know what django version i use? is it 1.0, 1.1, or 1.2?
- Finding a Python Library to Mock a Database
- In Django ORM, "values" and "annotate" are not working to group by
3👍
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.
3👍
Gosh! It drove me crazy! Just do the following!
python C:\python27\scripts\django-admin.py startproject mysite
- How to filter JSON Array in Django JSONField
- Database trouble in Django: can't reset because of dependencies
- Django querysets + memcached: best practices
- Django Storages – Could Not Load Amazon's S3 Bindings Errors
- Creating custom Exceptions that Django reacts to
2👍
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.
1👍
I’ve solved it! It’s the command line to open a file .py
.
It has to be like so:
"C:\Python27\python.exe" "%1" %*
mine was:
"C:\Python27\python.exe" "%1" without the final %*
I’ve used FileTypesMan
to edit because Windows 7 can’t edit this property.
1👍
In my case it was solved by adding the path to django-admin.py.
The instruction in windows with a python 7 and django 1.11 is:
python c:\Python27\Lib\site-packages\django\bin\django-admin.py startproject mysite
- Is there any way to use GUIDs in django?
- Method in Django to display all attributes' values belonging to a created object?
- Django Rest Framework: How to enable swagger docs for function based views
- Django command: How to insert newline in the help text?
0👍
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.
- Django update on queryset to change ID of ForeignKey
- Django: prefetch related objects of a GenericForeignKey
- How to upgrade Django on ubuntu?
- Django Enter a valid date. validation error
- Why use Django's collectstatic instead of just serving the files directly from your static directory?
0👍
After I installed an IDE, I had a similar (if not the same) problem. Sure enough, the .py “Open With” setting had been changed, and changing it back to the Python Launcher for Windows did the trick.
- How to convert a list of dictionaries to JSON in Python / Django?
- Convert Python None to JavaScript null
- Creation of dynamic model fields in django
- What are the valid values for a django URL field?
- Is there a way to make a block optional in Django template
0👍
Since the path is too long, I moved the folders to a shorter path inside C: /. This way I call it faster, there is a problem with the route. I use Windows 10 with Django 3.0.5, so it turns out like this: python C:\Python38\Scripts\django-admin.py startproject test
- Creating a .gitignore file for a Django website
- Is there a way to combine behavior of SESSION_EXPIRE_AT_BROWSER_CLOSE and SESSION_COOKIE_AGE
0👍
The problem for me was the installation of Visual Studio.
I tried to install django-admin, which is usely automatically set up:
pip3 install django-admin
and it gave to me :
error: Microsoft Visual C++ 14.0 or greater is required.
So, I install Microsoft Visual Studio, and then it works.
- Generating single access token with Django OAuth2 Toolkit
- Django Channels Error – Cannot import BACKEND 'asgi_redis.RedisChannelLayer'
- Celery periodic tasks not executing
- How can I escape LaTeX special characters inside django templates?
-1👍
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.
- Can I have some code constantly run inside Django like a daemon
- How does this Man-In-The-Middle attack work?