django-admin.py on win7 asked programm for open - django

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

Related

Python Tkinter throwing Tcl error

I am learning basic GUI in Python, and I came across a sample example to read file name from file explorer on Stack Overflow.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
This particular script is working fine when I am trying to run it in IDLE, but the same is not running if I am trying from command prompt in windows 7.
Python Version: 2.7. Here is the output error which I get.
>>> from Tkinter import Tk
>>> from tkFileDialog import askopenfilename
>>> Tk().withdraw()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1685, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Python27/lib/tcl8.5 D:/PyProj/lib/tcl8.5 D:/lib/tcl8.5 D:/PyProj/library D:/library D:/tcl8.5.2/library D:/tcl8.5.2/library
This probably means that Tcl wasn't installed properly
Any pointer to what I am missing here can be of great help.
You just need to copy two folders from tcl folder to the Lib folder
tcl8.5 and tk8.5
In case you are using Virtualenv on Windows I found a solution here: https://github.com/pypa/virtualenv/issues/93
I copied the "tcl" folder from C:\Python27\ over to the root of the new Virtualenv, Tkinter.Tk() shows a new window without throwing an exception.
I am running Python 2.7 on Windows 7.
Hit a similar problem after installing Activestate Python and TCL. I found the following page solved the problem for me: ActiveState Python install problem. The fix was to copy the contents of C:\Python27\tcl into C:\Python27\Lib.
Another potential solution (given by user i-shenl in a different ActiveState thread on the same issue) is to set the environment variable $TCL_LIBRARY to point to the tcl library folder ("C:/Python27/tcl", in the question). If you set this system-wide or account-wide (via System Properties), it will affect other programs that use a TCL Library (if any are installed). If you're using Powershell, you can set this variable in your profile to limit its affects to programs run from the shell.
I hit the same problem on Ubuntu 17.04 with virtualenvwrapper for 64 bit Python 2.7
I add tk and tcl library paths in local postactivate script
Go to your virtualenv: workon your-env-name
Edit local postactiave script with your favourite editor, for ex:
gedit $VIRTUAL_ENV/bin/postactivate
Locate tk and tcl library paths. In postactivate script, export TK_LIBRARY and TCL_LIBRARY with appropriate paths. Add this lines to your script with modified paths:
TK_LIBRARY=/home/kamil/anaconda2/pkgs/tk-8.5
TKPATH=/home/kamil/anaconda2/pkgs/tk-8.5
TCL_LIBRARY=/home/kamil/anaconda2/lib/tcl8.5
export TCL_LIBRARY TK_LIBRARY TKPATH
Restart your virtualenv: deactivate and workon your-env-name again.
If you are hitting this kind of error in a python -m venv NAME kind of virtual environment (and you actually have tcl installed in your system), then you need to export the paths similarly as suggested by Kamil Czerski in a previous post for virtualenv.
To find out what are your TK and TCL paths, run a python script outside of the venv (source):
import tkinter
root = tkinter.Tk()
print(root.tk.exprstring('$tcl_library'))
print(root.tk.exprstring('$tk_library'))
Open your venv configuration file bin/activate and find the place where they export PATH and insert after this (insert correct paths from step 1):
TCL_LIBRARY="/tcl/path/from/step/1"
TK_LIBRARY="/tk/path/from/step/1"
TKPATH="/tk/path/from/step/1"
export TCL_LIBRARY TK_LIBRARY TKPATH
Deactivate (if it was activated) and source again your venv:
deactivate
source bin/activate
The "Tcl missing"-error should be gone.
IDLE is probably setting the path required for TCL. To find out what path is being used by IDLE, compare the output of sys.path from IDLE and without IDLE. Then you can add the location of init.tcl either using an environment variable or programatically. See Xenomorph suggestion.
All you need to do is copy tcl 8.6 and tcl 8.5 from tcl file to Lib file on in python.
Python-tcl-tcl8.5 to Python-Lib
Go to directory in which all of your python dependencies are stored
Example:
Python37
-DLLs
-Doc
-etc
-include
-Lib
-libs
-Scripts
-tcl
-python.exe
Go to tcl folder, copy the tcl8.5 and tk8.5 folder
Paste these folders in the Lib folder
This solution works for Windows 10 users

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 is not working properly

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