I have ubuntu server , and my python virtualenv is under /var/www/abc/python folder.
and my code is under /var/www/abc/code folder.
Now i want to start virtualenv before i execute my code using shell script.
Here is the shell file runshell.sh , but it doesn't start the virtual enviorment.
source /var/www/abc/python/bin/activate
python /var/www/abc/code/app.py
Same as you do in shell
#!/bin/bash
source /var/www/abc/python/bin/activate
python /var/www/abc/code/app.py
Or you can directly run python from venv
#!/bin/bash
/var/www/abc/python/bin/python /var/www/abc/code/app.py
Related
I am using a launcher.sh for a raspberrypi 4B with Raspi os x64 to start a python script in a python env on startup. But setting the environment is problematic.
The file (test.sh) looks like:
. envname/bin/activate
echo Hi Stackoverflow
or alternatively
#!bin/bash
. envname/bin/activate
echo Hi Stackoverflow
using sh test.sh, correctly executes the shell script.
However, using it at launch with crontab
#reboot sh /home/pi/test.sh >/home/pi/logs/cronlog 2>&1
I get an error:
/home/pi/test.sh: 2: .: cannot open envname/bin/activate: No such file
How to properly set source #reboot to start the env on startup?
p.s. sh is not the solution.
With bin/sh and source envname/bin/activate it does not work as Ubuntu apparently uses bash rather than sh, and if sh is used the source command is not known. Bash uses . instead of source
Edit 1:
Normal Ubuntu uses bash for shell, but maybe that is different on startup? Could that be a way?
The answer from Goldilocks here tell us that the path has to be absolute, as crontab does not know where to find envname.
find | grep envname returned that it is at /home/pi/envname
so:
#!bin/bash
. /home/pi/envname/bin/activate
echo Hi Stackoverflow
does work.
They help me, they know I need to run a script to start the services, I use Django with Python and ubuntu server.
I have been seeing many examples in crontab, which I will use, every time I restart the server, I run the Script, which contains the command to run the virtual environment and in addition to the command "python3 manage.py runserver_plus", apart was to see restart the server all nights, I was also successful with crontab, but I can't execute what the script contains. They can help me, I am not very expert, but I managed to do something.
Is it the path of the script?
Tried running the command directly, got no results.
I write the following.
root#server:/home/admin-server# pwd
/home/admin-server
root#server:/home/admin-server# ls -l
drwxrwxr 3 admin-server admin-server 4096 Nov 20 17:25 control_flota
-rwxr--r-- 1 root root. 141 Nov 20 18:00 server_script.sh
Script new
I still have no results: /, I don't know why?
#!bin/bash
echo "Welcome"
cd /home/admin-server/control_flota/
source venvp1/bin/activate
echo "Thanks"
You can activate the Virtual Environment from within the shell script, prior to running any manage.py commands
#!/bin/bash
cd /your_code_directory
source env/bin/activate
python ./manage.py runserver_plus
Ensure you save the file with the .sh extension, then give it execute rights:
chmod u+x your_script.sh
You should then be able to call from cron; sudo cron if you run into permissions issues
I found this post to be useful on how to code a batch file to automate django web server start.
But the problem is, there is no virtualenv activated, How can i activate it before the manage.py runserver inside the script?
I would like to run this server with virtualenv activated via batch file.
Found my solution by encoding this:
#echo off
cmd /k "cd /d C:\Users\[user]\path\to\your\env\scripts & activate & cd /d C:\Users\[user]\path\to\your\env\[projectname] & python manage.py runserver"
Call the activate.bat script in your batch file, before you run manage.py,
CALL \path\to\env\Scripts\activate.bat
python manage.py runserver
try \path\to\env\Scripts\activate
and look at virtualenv docs
If your virtualenv is created via virtualenvwrapper:
workon yourenvname & python manage.py runserver
Normally I execute heroku run to set environment variables from a .env file. I can't prefix PyCharm's command with heroku run. How do I set environment variables?
Create python file in the root of your project.
Paste this inside
#! /usr/bin/env bash
export $(cat /vagrant/.env | grep -v ^# | xargs)
/home/vagrant/.virtualenvs/virtualenvname/bin/python "$#"
Make it executable chmod +x python
In pycharm set this file as the python interpreter for the project.
N.B. It needs your variables in .env to be seperated using an equals sign and for there to be no spaces.
eg.
DJANGO_SECRET_KEY=abc123abc124
DJANGO_SETTINGS_MODULE=myproject.settings.local
I am using MacOSX Yosemite, I am trying to execute a python code without always typing the path or getting into the folder. I tried the following :
1) Added the line #! /usr/local/bin/python (after finding where the python is found)
2) sudo chmod a+x full_file_path
But this does not work for me. Nor
export PYTHONPATH=full_file_path
How else can I execute the python script without actually getting into the directory. I cannot also execute the script without using ./ the chmod does not change the access to executable. Which as far as I have seen many forums. It should.
You need to add full_file_path to your shell PATH variable. It is your shell that does the searching for the script, not Python. Only when the script has been found, is Python being started:
export PATH="full_file_path:$PATH"
You can add that line to your .bash_profile or .profile file in your home directory to make this addition permanent.
Run these commands without the $ signs in the front:
$ ls -l /full/directory/progname.py
$ chmod +x /full/directory/progname.py
$ ls -l /usr/local/bin/python
$ export PATH="$PATH:/full/directory"
$ progname.py
If any of the ls commands display an error message, then you are looking for the file in the wrong place, and you have to find the correct location, and update the command accordingly.
It's important to note that /usr/local/bin/python can also be wrong, for example some systems have the Python interpreter in /usr/bin/python.