Activate python virtualenv and runserver in bat file with windows terminal - python

I'm trying to create a bat file to open windows terminal, activate an environment and run a server. After some attemps the best I've got is this:
wt.exe cmd -NoExit -c "c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1;cd C:\Users\me\Desktop\myProyect\;python manage.py runserver"
But windows terminal activate the environment in a tab and in other tab throw this error:
[error 0x80070002 when launching `"C:\Users\me\Desktop\myproyect\;python manage.py runserver"']
I think that maybe the command after the environment activation is not properly 'passed' to the environment... but really I don't know how to solve it.

-NoExit -c both look like powershell flags, not cmd ones. That might be a place to start.
I'd recommend just taking the entire script you've got there:
c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1;cd C:\Users\me\Desktop\myProyect\;python manage.py runserver"
and putting it into a .bat file like:
powershell -f c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1
cd C:\Users\me\Desktop\myProyect\
python manage.py runserver"
then running that with wt.exe foo.bat

Related

How do I run an existing Django file?

I downloaded a folder with multiple python files in the form of a Django project for a course and I am unable to run it. Usually, when you create a Django project it is created within the terminal with the line django-admin startproject web_project . and then python manage.py migrate is used and then the line python manage.py runserver to start a server.
How do I start a server to run my project?
When I use django-admin startproject web_project ., it creates a new project which I don't want. When I use python manage.py migrate or python manage.py runserver it says:
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
But even when I used python3 manage.py migrate or python3 manage.py runserver instead and it returned:
python3: can't open file 'manage.py': [Errno 2] No such file or directory
At first make sure you opened command prompt or terminal in right directory(in projects directory) when you are running the runserver command.
If you are on windows open the project folder(the folder that contains files like manage.py), then right click and press open PowerShell window here.
first of all I recommend creating a virtual environment using the following command
python3 -m venv .env
then activate it
source .env/bin/activate
"django-admin statproject" its for initalite a new project for running an excited Django app you should just install Django - if there is now "requirements.txt" file- , migrate and run.
pip install Django
python manage.py migrate
python manage.py runserver

How to launch PyCharm from WSL terminal?

Is there a way to run Pycharm from wsl terminal by typing the command like charm <file_name> or pycharm <file_name>, just like it is with vscode where you type code <file_name> ?
From this article on How to run Windows 10 programs in a WSL Linux shell, you can already run Windows programs from WSL ex. notepad.exe. This is because the program in the WSL path seems to be in sync with Window's path. You can add the path to the PyCharm executable to the path variable, C:\Users\trakw\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\221.5921.27\bin, restart WSL and run pycharm64.exe which start PyCharm.
If you're using JetBrains Toolbox to install PyCharm, C:\Users\trakw\jetbrains\bin, containing the batch file pycharm.cmd to run PyCharm, is already in the path variable.
Running pycharm.cmd in WSL will give errors because the .cmd file is written in batch which WSL doesn't recognize.
You need to run it using cmd.exe /c pycharm.
You can add the modified code below to ~/.bashrc file in WSL, so you can run cmd pycharm ..
# Usage : cmd pycharm .
cmd() {
CMD=$1
OPEN=$2
WIN_PWD=`wslpath -w "$(pwd)"`
WIN_OPEN=`wslpath -w ${OPEN}`
pushd /mnt/c;
cmd.exe /c "${CMD} ${WIN_OPEN}"
popd;
}
# Disable pushd echoing
pushd () {
command pushd "$#" > /dev/null
}
# Disable popd echoing
popd () {
command popd "$#" > /dev/null
}
There is still problem. This cmd pycharm . in WSL works with projects in \\wsl$ created in PyCharm. For non-project folder in \\wsl$, the error below will show up. However, pycharm . in cmd, works with any directory.
Yes. It's possible, if you using Windows 11:
https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

'No such directory' when activating a Pipenv on Windows

I've created a pipenv environment and now I'm creating a shell script called bootstrap.sh to activate it and run a flask server, but the following line is producing an error
bootstrap.sh reads as follows
#!/bin/sh
...
source "$(pipenv --venv)\\Scripts\\activate"
...
Running simply pipenv --venv returns C:\Users\johng\.virtualenvs\cashman-flask-project-2vwc8e6G
But executing the shell script returns a No such file or directory error
$ source bootstrap.sh
/Scripts/activate: No such file or directoryask-project-2vwc8e6G
This is in the vsCode bash terminal on Windows 10. The file certainly exists and can be navigated to, so what is causing this?

Start cmd and run multiple commands in the created cmd instance

I am trying to start cmd window and then running a chain of cmds in succession one after the other in that cmd window.
something like start cmd /k pipenv shell && py manage.py runserver the start cmd should open a new cmd window, which actually happens, then the pipenv shell should start a virtual environment within that cmd instance, also happens, and the py manage.py runserver should run in the created environment but instead it runs where the script is called.
Any ideas on how I can make this work?
When issuing commands chaining them, the system sees it as first command && second command
I your case, you are giving first command to be start cmd and second command py manage.py which will do exactly that, you are issuing a cmd in a new window and if that is successful, it will initiate py in the same window you started. You should therefore escape the & with a caret in order to pass through the chain to the second command window and not initiate the chain in the current window:
start cmd /k pipenv shell ^&^& py manage.py runserver
Keep in mind that you could also just add both commands into a batch file as:
pipenv shell
py manage.py runserver
and launch it as:
start "" mybatch.cmd
Your py manage.py runserver command calling python executor in your major environment. In your case, you could use pipenv run manage.py runserver that detect your virtual env inside your pipfile and activate it to run your command. An alternative way is to use virtualenv that create virtual env directly inside your project directory and calling envname\Scripts\activate each time you want to run something inside your virtual env.

How to set environment variables in Supervisor service

How do you export environment variables in the command executed by Supervisor? I first tried:
command="export SITE=domain1; python manage.py command"
but Supervisor reports "can't find command".
So then I tried:
command=/bin/bash -c "export SITE=domain1; python manage.py command"
and the command runs, but this seems to interfere with the daemonization since when I stop the Supervisor daemon, all the other daemons it's running aren't stopped.
To add a single environment variable, You can do something like this.
[program:django]
environment=SITE=domain1
command = python manage.py command
But, if you want to export multiple environment variables, you need to separate them by comma.
[program:django]
environment =
SITE=domain1,
DJANGO_SETTINGS_MODULE=foo.settings.local,
DB_USER=foo,
DB_PASS=bar
command = python manage.py command
Just do it separately:
environment=SITE=domain1
command=python manage.py command
Refer to http://supervisord.org/subprocess.html#subprocess-environment for more info.

Categories

Resources