DJANGO + windows: .bat file: open a command prompt and run the commands - python

I have to run the following set of commands after opening a windows cmd prompt
D:
cd Dev\project\backend
.\venv\Scripts\activate
python manage.py runserver
How to write a script(.bat) file for running these commands and the runserver will stay showing the output
I tried to put these commands in django.bat and double click, but a cmd window opens and within a fraction of second it closes. Dont know what happened

Instead of activating the venv then running manage.py separately, you should run directly using the python.exe from your virtual env.
It should look like this:
> D:\Dev\project\backend\venv\Scripts\python.exe D:\Dev\project\backend\manage.py runserver
I don't really know what you intent to use this for, but in windows you should run your Django project using IIS. Check out django-windowsauth, you can use it to run Django on IIS in few commands. https://github.com/danyi1212/django-windowsauth.
Just skip adding the middleware and the authentication backend of you don't need Windows Authentication on your site.

I tried to do this recently, but had to move onto something else quickly; given your question it's a nice opportunity to return to it, and I believe I've found the answer!
From what I found on my Laptop just now, the issue is with the activate line
.\venv\Scripts\activate
On Windows, this is another .bat file, and I suspect that calling it runs in a new cmd instance, rather than the current one
So I simply copied the entire contents of activate.bat and put it into my custom script, and it worked!
#echo off
cd D:\Dev\project\backend
-> Paste entire contents of activate.bat on this line <-
.\manage.py runserver

Related

git bash freezes when I run "python manage.py runserver"

I'm trying to learn django and have already created my project using startproject. Now I want to run the server but it won't work in git bash. It works if I use powershell or cmd but it just freezes in git bash. It says "Watching for file changes with StatReloader" but does nothing. Please help I want to use git bash to run my commands. Thanks
The Django auto reloading feature is recent: make sure to use the latest Git for Windows (v2.25.0-rc0.windows.1) to see if the issue persists in a git bash session.
If this is based on inotify to do file watching... that might not be compatible with mingw.

I can't open manage.py

On the command prompt, (Windows 10) I created a virtual environment, I then created a new project, I installed django in the virtual environment using pip. Then decided to run the command
python manage.py runserver
To run djangos web server,
This returned with
"...can't open file 'manage.py': [Errno 2] No such file or directory"
I did see a similar question to this on this platform but it seems to only apply to those using linux. Where answers specify to use the ls command, which is not applicable on Windows command prompt. I have tried this multiple times but i just can't open
manage.py
When running a command on the command prompt, it is necessary to provide the correct path to the file.
Let's pretend you have your django files stored in a folder with the following path:
C:\my_stuff\subfolder\django_project >
Presume that when you run the dir command (the Windows analog to the ls command you mention in your question) it might show something like this:
C:\my_stuff\subfolder\django_project > dir
blog
db.sqlite3
manage.py
mysite
If you were to then run your command:
C:\my_stuff\subfolder\django_project > python manage.py runserver
Everything should work fine. But if you are in a different directory, you will get the error you describe. For example, this will fail.
C:\my_stuff\subfolder > python manage.py runserver
If, for some reason, your Windows command prompt does not show you which directory you are in, you can run the cd command to confirm where you are.
An alternate method is to point the python interpreter to the location of the manage.py script, by providing the path:
C:\my_stuff\subfolder > python django_project\manage.py runserver
But this sometimes introduces subtle problems, because scripts like manage.py might be written with the expectation that they will be run from the directory they are stored in. Your mileage may vary.

Django-admin.py startproject is not being able to be created

whenever i give
django-admin.py startproject
i get a notepad of django-admin. So that means i type in my website inside that ?
i have executed the django-admin.py and django-admin.pyc in cmd also. Still it shows. What should I do ?
You have - for some reason - created a virtual environment inside the actual Python source directory on Windows.
There is nothing wrong with this, except you may face issues when you uninstall or upgrade Python.
To fix your immediate problem, you need to follow these steps:
Close all command prompt windows.
Open a new command prompt.
Type the following and hit ENTER which will activate your virtual environment:
C:\Python27\Scripts\pymote_ven\Scripts\activate.bat
Now, type the following to start your project:
python django-admin.py startproject nameofyourproject
You should also read the documentation because you have confused the directories that are involved.

Django /manage.py runserver doesn't work (Windows)

I have installed python 2.7.10 in windows. I installed django in path c:python27/scripts/with a command pip install django and created project with command django-admin startproject mysite from the same path.
Now to run server i cd to path c:python27/scripts/mysite and ran a command manage.py runserver/ manage.py runserver 0.0.0.0:8000 And this has no any effect.
where did i go wrong, and also i couldn't run with python console. and i couldn't redirect to my project from python CMD. all i did is from windows console.
Edit:
Screenshot of execution
First step was to set the environment variable.
windows key + pause or Control Panel\System and Security\System
Advance system settings (this will open system property)
navigate to Advanced tab > Environment variable
Edit path - append ;c:\python27 in variable value field
Restart CMD
then /python manage.py runserver should work
Trying setting up a virtualenv for your project.
This same issue happened to me when trying to launch the test server
python .\manage.py runserver
from PowerShell on Windows 10. According to the Django site, there might be an issue with the type of arguments being passed from PowerShell.
My workaround was to use a virtualenv. Once that was setup with django installed via pip, the runserver command worked.
The best solution is to install Python from Microsoft Store. In this case, you won't have to worry about the Environmental Variables and Path. Windows will detect all that automatically.
Try this fix guys:
1. Right click on the windows icon/start on the bottom left and run Windows Powershell as admin.
2. Than type cd ~/ and later change the path again to the project folder.
3. type python manage.py runserver and press enter.
had the same problem. fixed it by checking python and django version compatibility. If you're still battling with this update one or the other or ensure they're both compatible with each other in the virtual'env' you're setting up.
good luck.
I think you forgot to add python to environment variables. So, During the installation, click the checkbox named "Add Python 3.9 to PATH" to add in environment variables. or you can simply add the path later.
When you open the command prompt on windows, the default directory might be C:\WINDOWS\System32>
Here, you have to change the directory by just adding cd to the default directory. Then copy the directory of where your project is and paste with one space. So it will be:
C:\yourfolder\yourproject>
Next, use the comman which is, python manage.py runserver
That's all 😅
After setting C:\Python in the environment variables, issuing the following command helped:
py manage.py runserver

A Python script that activates the virtualenv and then runs another Python script?

On Windows Vista, I need a script that starts the activate (to activate the virtualenv) script in:
C:\Users\Admin\Desktop\venv\Scripts\
And later, in the virtual environment, starts to the manage.py runserver in the folder:
C:\Users\Admin\Desktop\helloworld\
How should I do? What modules should I use?
You can activate your virtualenv and then start server using a bat file.
Copy this script in to a file and save it with .bat extension (eg. runserver.bat)
#echo off
cmd /k "cd /d C:\Users\Admin\Desktop\venv\Scripts & activate & cd /d C:\Users\Admin\Desktop\helloworld & python manage.py runserver"
Then you can just run this bat file (just double click) to start the server
runserver.bat:
CALL [your path]\Scripts\activate.bat
python manage.py runserver
If you want call virtualenv'ed Python directly you can do something like this:
C:\Users\Admin\Desktop\venv\Scripts\bin\python.exe manage.py runserver
Double check python.exe location on your virtualenv folder - don't remember how it is out of my head. This Python associates itself with the virtualenv and uses its site-packages by default.
I am using Anaconda 3 and python 3.7.6 on Windows. Had to do this in my .bat file:
CALL path\to\base\virtual\environment\Scripts\activate.bat path\to\your\virtual\environment
[path\to\your\virtual\environment]python.exe path\to\your\script\yoursript.py
Without activate.bat nothing works. I was getting an error about mkl-server. This error is described here https://github.com/numpy/numpy/issues/15523. People complained there about conda being broken, i.e. just calling python.exe yoursript.py does not work.
For me the above didn't work and therefore I will provide a more general answer.
But first specifically, this worked for me:
Open a notepad
paste this:
#echo off
CALL c:\1\env\Scripts\activate.bat
python c:\1\app.py runserver
save as whatever.bat
double-click this file to run
And generally: it is important to locate "activate.bat" under your python project. My project in this case was in c:\1 and the activate.bat under the relative directory env\Scripts which apparently may be situation dependent or have changed over time. This makes the general script:
#echo off
CALL [Your python project path]\[the relative path of your activate.bat]\activate.bat
python [Your python project path]\[your python filename].py runserver
In my case the project path was: c:\1
The relative path: env\Scripts
And the python filename: app
When I make a virtual environment the env files are placed relative to my python file. Just in case your situation is like in the question the call line in the script would change to
CALL [your activate.bat location]\activate.bat
i.e. in this situation the following should work:
#echo off
CALL C:\Users\Admin\Desktop\venv\Scripts\activate.bat
python C:\Users\Admin\Desktop\helloworld\manage.py runserver
Tip: I just found that python took my desktop as the working directory. It may therefore be a good idea to change your working directory to your python path. In my case adding cd\1 under #echo off does that trick.
Rather than using strings you can use a caret (^) as described in this question: Long commands split over multiple lines in Windows Vista batch (.bat) file
E.g.
cmd /k cd path/to/activate ^
activate.bat
pip uninstall --yes package ^
pip install git+https://git.server.com/user/project#remote/branch ^
deactivate
will open a venv and uninstall and reinstall a branch of a Git repository. This is a useful pattern for automating deployment of code into a venv.
For me, working with this code: (script_file.bat)
#echo off
CALL C:\Users\apo1979\Anaconda3\Scripts\activate.bat PyPWBI
C:/Users/apo1979/Anaconda3/envs/PyPWBI/python.exe "d:/.APO_OneDrive/script_SpeedTest.py" runserver
pause

Categories