django-admin.py is not working properly - django

I have just spotted that something is wrong with my django-admin.py command. I checked similar SO posts on django-admin.py problems but nothing seems to be related to my problem. I use Windows Vista (yeah, I know...). I also have many versions of django in some folder on my disk and I switch to the version I need using junction command (this is similar to symlinking in unix), I don't have problems with this and never had problems before.
I used django-admin.py many times before but now for some unknown reasons I got this info (Django 1.1.1):
C:\>django-admin.py startproject some_project
Type 'django-admin.py help' for usage.
and suprisingly when I type what django asked me to type:
C:\>django-admin.py help
Type 'django-admin.py help' for usage.
????
When I switch to Django 1.2.1 I got this:
C:\>django-admin.py startproject help
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=all output
--settings=SETTINGS The Python path to a settings module, e.g.
"myproject.settings.main". If this isn't provided, the
DJANGO_SETTINGS_MODULE environment variable will be
used.
--pythonpath=PYTHONPATH
A directory to add to the Python path, e.g.
"/home/djangoprojects/myproject".
--traceback Print traceback on exception
--version show program's version number and exit
-h, --help show this help message and exit
Type 'django-admin.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
cleanup
compilemessages
createcachetable
...
startproject
...
validate
no matter if I type startproject help or startproject some_name it always shows the same message.
Any ideas?
EDIT: new info
I've just noticed that my command line arguments are not visible when command is parsed by django managament utlity (when I print command argv it shows only path to django-admin.py without any of given arguments)

The problem in my case was the I've lost .py file association rules.
If you have similar problem you can check in command line what is the result of:
assoc .py
If there is no such association you need to create it:
assoc .py=Python.File
and create rule for Python.File:
ftype Python.File="c:\python27\python.exe" "%1" %*
(Replace with whatever the path is to your python interpretter.)
now django-admin.py accepts command line args and everything works fine!

This would be better as it does not require touching the registry at all:
https://stackoverflow.com/a/10732170/1585863

Related

Calling Python package from command line / PyCharm

I am creating a Python package, I anticipate that it will be called both by command line and from other scripts. Here is a simplified version of my file structure:
GREProject/
__init__.py
__main__.py
Parsing.py
Parseing.py contains a method, parse(), it takes two arguments, an input file and an output file. I am trying to figure out the proper code for "__main__.py" so that when the following is called from the command line or terminal the arguments will be passed to "parse()":
Python GREProject -i input.file -o output.file
I have tried this numerous ways but they have all met with failure, I do believe I need the "-m" flag for the interpreter but more than that I don't know. Example with the flag:
Python -m GREProject -i input.file -o output.file
When running the later command I receive the following error:
Import by filename is not supported.
Presumably from this line:
from . import Parsing
Ok, turns out this was a problem with my IDE, PyCharm. No idea why I recieved this error but I have setting that fixed it:
Import by filename is not supported.
For the record here are the options I set in my Pycharm project
Script:
GREProject
Script parameters:
-i .\GREProject\pr2.nyc1 -o .\GREProject\Test.pkl
Enviroment variables:
PYTHONUNBUFFERED=1
Python interpreter:
Python 2.7.11 (c:\Python27\python.exe)
Interpreter options:
-m
Working directory:
C:\Users\probert.dan\PycharmProjects
Here is an explanation of the options:
Script: This is the script to run, by default PyCharm will only insert absolute references to .py files, nothing prevents you from manually typing in a relative reference, in this case it is the GREProjects folder.
Script Parameters: These are passed onto the script itself, in this case I am telling my script that the input file is ".\GREProject\pr2.nyc1" which means, look the file "pr2.nyc1" in the "GREProject" directory below the current working directory.
Environment variables: This was set by PyCharm and left unchanged.
Python interpreter: My active interpreter.
Interpreter options: The option here tells python that we are calling a module, python then knows to access the "__main__.py" file.
Working directory: The directory the script is run from, I chose the directory above "GREProject"
For reference here is the contents of my "__main__.py file":
from . import Parsing
import argparse
parser = argparse.ArgumentParser(description='Parse flags.')
parser.add_argument('-i', help='Import file.')
parser.add_argument('-o', help='(Optional) Output file.')
arguments = parser.parse_args()
Parsing.parse(arguments.i, arguments.o)
It is also important to note that debugging in PyCharm is not possible like this. Here is the solution to debugging: Intellij/Pycharm can't debug Python modules

Django Install Wierdness - Mac OSX 10.8.5 and Macport Python 2.7.6 - Django/Python/Unix beginner

I've found a lot of posts on Macports/Django install issues but none seem to quite address my situation.
Installed Django using Macports from the command line using
sudo port install py27-django
This seemed to work fine. I opened up IDLE and was able to import django. The following bit of code
import django
from django.core import management
print django.VERSION
returns
(1, 5, 1, 'final', 0)
Which I take to mean I have the final version of Django 1.5.1 installed. So, all seems to be well.
However, now I switch over to my Django tutorial to get learning and I am asked to type this into the command line to confirm that Django is installed:
python -c "import django; print(django.get_version())"
and get the following error:
> File "<string>", line 1, in <module> ImportError: No module named
> django
Some other bits of data. Here's my $PATH:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin
Finally, the command
django-admin.py startproject mysite
returns
-bash: django-admin.py: command not found
But the command
django-admin-2.7.py startproject mysite
works (creates a new directory called mysite in the working directory). I read somehwere that Macports renames the django-admin file.
I've only been working with Unix for a few weeks so this is a bit confusing. It seems like I have Django on my system, but perhaps the python I am trying to access from my command line is a different python from that which Macports uses.
Any help appreciated!
Dennis
You will need to set your PYTHONPATH environment variable from your terminal to reference your django install. IDLE probably did this for you, but in another shell you'll need to set it. You can set it in your terminal permanently by setting it in ~/.bashrc or ~/.bash_profile (or whatever your terminal shell is).
Your PATH starts with /Library/Frameworks/Python.framework/Versions/2.7/bin so when you type python you get the python in that directory which is the Apple supplied one nut you have installed Django for the Macports one so python does not see it.
To fix you need to have /opt/local/bin earlier on the path than the /Library one. even better remove that /Library entry and use port select python ... to choose which python to run the Macports 2.7 or Apple's or others
As for the django-admin-2.7.py issue, Macports allows you to have several versions of python at once so code depending on eth version has the version add into the script names e.g. you could have a django-admin-2.6.py as well if you installed the py26-django package. This also applies to python itself however python also adds into the port select mechanism so you set a default version.

No module named django.core when creating project in virtualenv

So I have looked around at a lot of questions similar to mine, however I couldn't find a concrete answer. My comp specifications are Windows 7 64-bit.
My problem is as such:
1) I installed virtualenv using pip:
pip install virtualenv
2) After that I created and activated a new environment:
path/virtualenv env
...
path/to/env/Scripts/activate
3) While running the new environment, I installed django:
(env) path/pip install django
4) After installing successfully, I am ready to make a project. However, upon trying it out:
path/django-admin.py startproject test
I get the following error:
File "C:/path/env/Scripts/django-admin.py", line 2, in (module)
from django.core import management
ImportError: No module named django.core
I have tried out various solutions people have posted, including using the full path:
python C:/path/to/django-admin.py startproject test
I have also checked to make sure the versions of Python it is referencing are correct,as both inside and outside the virtualenv it is associated with Python27. Many other solutions talked about PYTHONPATH or the syspath, however, when I import django or managemnet in the python shell, those work fine.
I have a feeling it may have something to do with the paths, but I'm not sure how a virtualenv interacts with the system paths. Since it is self-contained and the system paths are system wide, is it necessary to have something in the path specifically?
As an aside, my django-admin.py file is in both
path/env/Scripts
and
path/env/Lib/site-packages/django/bin
and the django folder is in
path/env/Lib/site-packages
How to fix this problem?
I solved this problem by using this command as following instead:
django-admin startproject
just remove the ".py" attached to "django-admin"
I could not get any other stack overflow answers to work either. Getting a venved Django stack running on Win64 is a bit of an ordeal.
But, I found an answer that worked for me here: http://samudranb.com/2012/06/02/how-to-setup-a-djangopython-development-env-on-windows/
Try running from an admin command prompt:
ftype Python.File="[your venv path]\Scripts\python.exe" "%1" %*
Just be sure to set it back to the original value when you're done.
This will help you understand why your facing that problem and there is also simple solution for that:
http://blog.jayteebee.org/2009/07/importerror-no-module-named-djangocore.html
I've literally searched for hours to a solution for this issue... I came across this video randomly: (https://www.youtube.com/watch?v=lPmkl4jtYgA) where he put "python .\Script\django-admin.py startproject" into the command prompt while in a virtual environment, so I tried the same with the following modification to point to the correct path on my machine "python .\env\Script\django-admin.py startproject". Voila!
Hopefully this helps someone as it seems there are multiple reasons for this issue.
Windows server 2003 provides the Where command
where python.exe
will show the full path of the current python.exe found on the path, use that to check it's using the correct one for your virtualenv.
The association issue comes into play because running
file.py
so the .py is argv[0] passes it through the windows association, which won't follow your venv.
python file.py
will not find file.py unless it's in the current directory.
So the solution is -
python %VIRTUAL_ENV%\scripts\django-admin.py startproject myproject
This runs python from the current active venv
and uses the venv env variable so it points to the correct location of django-admin.py (or you could give it an absolute path yourself of course)
I had the same problem. I solved that using this command:
(env)C:\environment directory>python Scripts\django-admin.py
This link was helpful for me:
enter link description here
i had the same problem i am running both python 3.4 and 2.7, so i pip installed Django globally on my machine and when i returned to my virtual environment i was able to create a project with no problems.
In Windows, you set:
set PATH=C:\virtualenv\python2.7\Scripts REM Scripts folder contains python.exe, pip.exe, django-admin.exe,...
set PYTHONPATH=C:\virtualenv\python2.7\Lib\site-packages REM site-packages folder contains packages of python such as django,...
After, create project mysite by
django-admin.py startproject mysite
I had this same problem using virtualenv in Terminal in MacOSX (Snow Leopard). My solution to the problem was to change the first line of django-admin.py from
#!/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
to
#!/path-to-your-virtualenv-directory/bin/python
Hope this helps someone.
I had the same problem and the way I resolve it was by activating my project directory before making migrations and running my server "python manage.py runserver".
Activate project Directory: source /path/bin/active
Path meaning where your project is stored.
Had same error and This solved it out for me.
source /path/to/virtualwrapper/activate
pip install django
This fix tries to re-install and configure django
I had the same problem, but I solved it, first I activated the virtual env, then ran:
django-admin.exe startproject project_name

How to create a project with Bitnami DjangoStack?

I just installed the Bitnami DjangoStack and want to know how to create a new project in Windows.
I tried opening the python.exe (similar to command prompt) and tried typing in:
django-admin.py startproject testproject
which is what all the articles seem to say to start with.
I am getting an invalid syntax error. I'm assuming I'm missing something very basic?
Thanks!
You need to run django-admin.py from the command prompt.
The Python interactive interpreter (what you get when you open up python.exe) is for typing in Python code and running it line by line. If you want to run Python code that is in a file, you need to run it from command prompt.
Note that if that doesn't work, you might need to put python before it, and you also might need no specify the full path (usually something like python C:/Python27/Scripts/django-admin.py, assuming you're on Python 2.7.)

django-admin.py on win7 asked programm for open

I have installed Django on Win7 and try
>>> import django
>>> django.VERSION
(1, 2, 3, 'final', 0)
it works. But when I call django-admin.py startproject myappname, Windows show me dialog with ask what program it should use to open this command. If I check python.exe then django-admin.py always show me help about django-admin.py
Windows show me dialog with ask what program it should use to open this command.
You can avoid this by using Python explicitly, provided Python.exe is in your PATH. For e.g.
python /path/to/django-admin.py startproject myappname.
If I check python.exe then django-admin.py always show me help about django-admin.py
From the documentation for django-admin.py startproject:
This command is disabled when the --settings option to django-admin.py is used, or when the environment variable DJANGO_SETTINGS_MODULE has been set. To re-enable it in these situations, either omit the --settings option or unset DJANGO_SETTINGS_MODULE.
Can you check if you have set an environment variable named DJANGO_SETTINGS_MODULE? If yes, remove/unset it and try again.
I have had the same problen that raise this error when I ran django-admin.py on Command prompt I couldn't create a projects.
I tried to Run Regedit by using Find to Search python.exe and then adding %% but it was not sufficient... I then added the necessary Path to the System PATH as Shown Below.
(Control Panel -> System -> Advanced ->Environment Variables -> PATH). Add to the PATH
C:\Python27\python.exe;C:\Python27\Scripts;C:\Python27\Lib\site-packages\django\bin
Adam It Work!!! Now I can Create Django Projects and applications Using Command on Windows 7