Bad python path in bash script from crontab - python

I would like to use the python3 executable when I use it from crontab as manual launch (ssh session).
bash script
#!/bin/bash
PYTHONPATH="$(which python3)"
echo $PYTHONPATH
python3 test.py
result from ssh command line, launched manually
/usr/local/bin/python3
result in log file from crontab -e
/usr/bin/python3
I would like the script launched by the crontab, uses /usr/local/bin/python3 executable instead of /usr/bin/python3
OR
if it's not possible, use the dependencies of my code available for /usr/bin/python3
How can I achieve this ? Thank you very much for your help

the python inside the docker container will not necessarily have the same path. If you want all the modules installed on your VM python3, create a requirements.txt file using pip freeze > requirements.txe and COPY this file as part of your Dockerfile and install it while building the image pip install -r requirements.txt

Related

Activcate a python virtual environment from a linux bash script

I am writing a bash script in linux that creates and activates a Python venv and then installs from a requirements.txt. Like this
python3 -m venv ~/myvenv/env
source ~/myvenv/env/bin/activate
cp requirements.txt ~/myvenv/env/requirements.txt
pip3 install -r ~/myvenv/env/requirements.txt
This doesn't work for me. It seems to create the myvenv directory but then doesnt switch in and run the requirements.txt file.
Is there a different way to activate it with source from within a bash script?
When you run script your shell spawns new process, activates it and then dies.
That's why when you get back into your shell you see unactivated one.
you can run your script just using source command. source command will load it into your active shell.
source script.sh

How to make packages using cli commands?

I tried using bitmex-market-maker python package.
Here's how to use this package.
Install: pip install bitmex-market-maker. It is strongly recommeded to use a virtualenv.
Create a marketmaker project: run marketmaker setup
I learned how to make my own command-line commands using python here.
To make my own commands, however, I had to type several terminal commands like
$ chmod +x marketmaker.py
$ mv marketmaker.py marketmaker
$ mkdir -p ~/bin
$ cp marketmaker ~/bin
$ export PATH=$PATH":$HOME/bin"
But I didn't type this commands at all but only installed bitmex-market-maker package using pip.
Can you help me figuring out how things work behind the scene?
I post marketmaker codes.
#!/usr/bin/env python
from market_maker import market_maker
market_maker.run()

Activating a python virtual environment within a bash script fails with "sudo: source: command not found"

I'm trying to automate the deployment of my Python-Flask app on Ubuntu 18.04 using Bash by going through the motion of preparing all the necessary files/directories and cloning the source code from Github followed by creating the virtual environment, installing the pre-requisite modules and etc.
Now because I have to execute my Bash script using sudo, this means that the entire script will be executed as root except where I specify otherwise using sudo -u myuser and when it comes to activating my virtual environment, I get the following output: sudo: source: command not found and my subsequent pip installs are all installed outside of the virtual environment. Excerpts of my code below:
#!/bin/bash
...
sudo -u "$user" python3 -m venv .env
sudo -u $SUDO_USER source /srv/www/www.mydomain.com/.env/bin/activate
sudo -u "$user" pip install wheel
sudo -u "$user" pip install uwsgi
sudo -u "$user" pip install -r requirements.txt
...
Now for the life of me, I can't figure out how to activate the virtual environment in the context of the virtual environment if this makes any sense.
I've scoured the web and most of the questions/answers I found revolves around how to activate the virtual environment in a Bash script but not how to activate the virtual environment as a separate user within a Bash script that was executed as sudo.
That's because source is not an executable file, but a built-in bash command. It won't work with sudo, since the latter accepts a program name (i.e. executable file) as argument.
P.S. It's not clear why you have to execute the whole script as root. If you need to execute only a number of commands as root (e.g. for starting/stopping a service) and run a remaining majority as a regular user, you can use sudo only for these commands. E.g. the following script
#!/bin/bash
# The `whoami` command outputs the current username. Unlike `source`, this is
# a full-fledged executable file, not a built-in command
whoami
sudo whoami
sudo -u postgres whoami
on my machine outputs
trolley813
root
postgres
P.P.S. You probably don't need to activate an environment as root.

AWS Elastic Beanstalk (eb) installation in Ubuntu 14.04: command not found

Im trying to install AWS eb command line interface in Ubuntu 14.04. I just donwloaded the .zip file. Extracted in a folder. if I go to folder where eb is (/home/roberto/app/AWS-ElasticBeanstalk-CLI-2.6.1/eb/linux/python2.7) and run it, I get: eb: command not found
Same if I do it with python3 path.
Fixed:
I just ran the command on a terminal:
$ export PATH=$PATH:/opt/aws/eb/linux/python2.7/
and it's working.
I think all you have to do is, upgrade awsebcli by running: pip install --upgrade awsebcli
If you are using arch linux:
Find your shell's profile script, in case of bash use .bash_profile
run
$ ls -a ~
$ nano profile_script` #in my case `$ nano .bash_profile
Add
export PATH=~/.local/bin:$PATH
Save and exit.
Now run
$source ~/profile_script
In ubuntu we write like :
export PATH=$PATH:/usr/local/lib/python2.7/site-packages/
It worked for me after writing this because eb folder will be present inside mentioned folder.

Using forever.js with Python

Two questions:
Is there a Python equivalent to forever.js to run a Python process in the background without requiring sudo?
Is it possible to use forever.js with Python? How about with a virtualenv?
It is easy to use Python with forever.js:
forever start -c python python_script.py
To use it with virtualenv is a little bit more complicated, I did it using a bash script (call it python_virtualenv):
#!/bin/bash
# Script to run a Python file using the local virtualenv
source bin/activate
bin/python $#
Now use that script with forever:
forever start -c ./python_virtualenv python_script.py
I was having problems executing a python script with custom logging paths, after trying I got to work with the next command:
forever start -c python -l /tmp/forever.log -o /tmp/out.log -e /tmp/error.log python_script.py
Tell me if it worked for you
Using python 3 with Flask to run with forever.js, here is my build process
python3 -m venv venv
source venv/bin/activate
sudo -H pip3 install -r requirements.txt
FLASK_APP=app.py forever start -c python3 app.py --port=5001

Categories

Resources