How to create a project with Bitnami DjangoStack? - django

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.)

Related

Termux says "'Bad Interpreter: No such file or directory"

I have a problem and hope someone can help me. I am currently trying to write a script for Termux or Termux:Task. My script currently looks like this:
#!/data/data/com.termux/files/usr/bin/bash
cd /./sdcard/www/public/
wp post list sleep 5
Every time I load the script I get the following error message:
/data/data/com.termux/files/usr/bin/wp: /usr/bin/env: bad interpreter: No such file or directory.
I've been looking for a solution to my problem for hours, unfortunately without success.
I am using an extension for Termux called "WordPress CLI". When I start termux and enter the commands individually, everything works. But as soon as I write the commands into a sh script and start it doesn't work anymore. :(
Can anyone help me?
Thanks a lot
This is simple error you can fix it by replacing !/data/data/com.termux/files/usr/bin/bash. With #!/data/data/com.termux/files/usr/bin/bash
Please tell if you get error again
Try with #!/usr/bin/env bash in the shebang line.
Termux-exec allows you to execute scripts with shebangs for traditional Unix file structures. So shebangs like #!/bin/sh and #!/usr/bin/env python should be able to run without termux-fix-shebang.
From https://wiki.termux.com/wiki/Termux-exec
According to doc:
Why do I keep getting a '/bin/sh bad interpreter' error?
This error is thrown due to access script interpreter at nonexistent
location.
Termux does not have common directories like /bin, /sbin, /usr/bin at
their standard place. There is an exception for certain devices where
/bin is a symbolic link to /system/bin, but that does not make a
difference.
Interpreters should be accessed at this directory only:
/data/data/com.termux/files/usr/bin
There are three ways to fix this:
Install termux-exec by using pkg install termux-exec. It won’t affect the current session, but after a restart should work without
any setup. Not needed if your Termux is up to date. If still not
working, try the next workaround.
Use command termux-fix-shebang to fix the shebang line of specified file.
Use termux-chroot from package proot to setup a chroot environment mimicking a normal Linux file system in Termux.
termux-fix-shebang my_script.py of second method work for me, which it modify the shebang(first line of my_script.py) from #!/usr/bin/env python to #!/data/data/com.termux/files/usr/bin/env python. Since /usr/bin/ is not exist in Android, that's why it throws the error /usr/bin/env: bad interpreter: No such file or directory. The other solution is run with python my_script.py, neither of my_script.py nor ./my_script.py.
In my test, termux-exec of the first method only work if I added correct shebang in main script(child OR child of child script no need) and ran command export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so.
And for the issue of this question, error shows /usr/bin/env in the middle with /data/data/com.termux/files/usr/bin/wp even though the shebang of script #!/data/data/com.termux/files/usr/bin/bash looks ok, it means that wp command (located at /data/data/com.termux/files/usr/bin/wp) used inside the script contains shebang #!/usr/bin/env wp and should modify it to #!/data/data/com.termux/files/usr/bin/env wp too. termux-exec of first method should fix this specific case too(already has correct shebang in main script).

E0401:Unable to import 'django.db'

So i'm studying python crashcourse and after finishing the basics I jumped to the django project.
I created a virtual env, installed django and created an app with manage.py startapp command.
I use Visual Studio Code, and the problem is when I go to models.py it gives an error that it's unable to find django.db module
unable to import error
Now I understand this is because of the file structure, which is as per my research the correct one, and I followed the book carefully, however i don't know how to fix this and I feel that i'm missing something.
This is my file structure for the virtual environment
file structure
Any idea how to fix it ? :)
Thanks in advance
Thank you Incognos !
To be more specific The solution was :
enter "code ." in the terminal and then a new VS opens.
Ctrl+Shift+P -> Select Interpreter
select the environment that starts with ./env or .\env
run Ctrl+Shift+` to activate it
VS is not seeing your virtualenv. You need to activate it for VS code so that it knows that django is installed in the virtualenv see https://code.visualstudio.com/docs/python/environments
it happens because pylint is installed outside your virtual environment. So install pylint inside virtual environment with this command (pip install pylint).
Note: If you're using Visual Studio Code - Insiders you'll use code-insiders . instead of the code . command.
Then, a new window will open and you can set your virtual environment interpreter there.
Reference: https://github.com/Microsoft/vscode/issues/6627
in my case, the problem was solved by opening VS-Code in the directory where virtualenv is located.
Click Ctrl-shift-p to access the Command pallete, then search for Python: Select Interpreter. Click on it and then select on the one that has been recommended by the editor.

Beginner Python spam.py

I am new to python, just got the learning python book and got stuck with the spam.py in the command line. The book says to make a file named spam.py and then ask python to run this by typing
%python spam.py
I have added the python to my PATH as it was C:\Python27 so I can call Python in the Windows CMD, but this just will not run. The error I receive is
>>>python spam.py
File "<stdin>", line 1
python spam.py
^
SyntaxError: invalid syntax
I appreciate any help that you can give.
Your problem is that you're trying to run your code from within the Python interpreter itself (the >>> prompt is the giveaway here since that's the Python prompt).
Exit from the interpreter (with CTRL-Z and ENTER for Windows) and run it from cmd.exe (the c:\> is the prompt in the example below):
c:\> python spam.py
From within the interpreter, you can also run an external file with:
execfile('spam.py')
Could you post the code from spam.py...
You seem to be trying to run the spam.py from the Python interpreter. Go to where the file is in Windows Explorer and launch it from there, using the C:\Python2.7\python.exe CLI.
By the way, since you didn't understand the syntax error warning, please see:
http://dictionary.reference.com/browse/SYNTAX
http://dictionary.reference.com/browse/SEMANTICS
You are trying to execute Python script file within the interpreter. Come out from the Python interpreter by pressing CTRL+Z and then ENTER key.
Then execute with the command :
Say, C:/> python spam.py
In order to run a python program you have to run program in Command Line not in Python Interpreter (press Windows Sign + R and type cmd.exe)
Moreover you have to remember to be exactly in the directory where your file is saved, e.g.:
If the file file is C:\Python27\spam.py you have to be in C:\Python27.
To change the directory:
use dir to display the folders and files in current place
use cd to change your directory (e.g. C:\Python27\>cd Spam moves you to C:\Python27\Spam
use Tab key to autocomplete names of the commands, folders and files
As you said you have added Python to PATH and followed my instructions, the statement below should work perfectly
python spam.py
Hope I could help.
If for example, your file is in G: drive, type this in cmd:
python G:/myfile.py
Basically, type in the path. Just doing "cd" won't work in Python

How do I run my Python program that I wrote?

I am trying to run my celcius.py file but I don't know what to type in the command line...
When I type Python celcius.py it does not work:
I get the following message:
>>> python celcius.py
File "<stdin>", line 1
python celcius.py
^
SyntaxError: invalid syntax
>>>
When I open it the program with the Python IDLE GUI it runs the program and closes the program before I can see it. I read somewhere that it won't close if you run it from the command line.
You need to type "python celsius.py" in the windows command prompt, rather than the Python command prompt; this only works straight away if the Python directory is in your PATH. It probably isn't, so you need to use this instead: C:\Python27\python.exe celsius.py.
Because celsius for you is in the same folder as python, you can do this as well:
CD C:\Python27
python.exe celsius.py
In general, change the directory (CD) to where your file is stored, then run C:\Python27\python.exe <filename>
Double-clicking is generally easier in Windows, though, assuming it is set up to do that (it normally is unless you change it back)
There are a few ways of doing this.
WAY 1
step one: Open command prompt (go to start and type in "cmd") *NOTE: do NOT open python. if you see ">>>" in your command prompt window you opened python. This is bad.
step two: type in this: python "path\to\file\celcius.py" replacing path\to\file with the path (starting with the name of the folder that you're computer user is named. So for my computer that would be ben. So let's say my celcius.py is located on the desktop I would run python "Desktop\celcius.py" and hit <.enter.>. It looks like you have celcius.py in your python folder. Don't put it there. that's the system files for python itself. move it to your desktop or your documents folder. By the way, if you have truble using this, make sure you're system is set up to execute the python command. Go here for reference.
WAY 2
assuming you have python associated with the .py file in explorer, you can find the file and double-click on it.
WAY 3
open IDLE (you can usually find it in your list of programs in start). Open the python file through IDLE. This will show you the source code for your program. hit <.F5.>. F5 will execute the program from within the the IDLE shell.
You don't need to do this, just open the command prompt and type:
python path/to/your/file.py
Just make sure that python command is set in your environment PATH, check this

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