/init.d/function + python version - python

I am trying to daemonize celery and celerybeat. I have downloaded the celeryd and celeybeat files from github and placed them in /etc/init.d/ (celery and celerybeat) with the corresponding config files under /etc/default/.
My problem is that when I run these two files, celeryd and celerybeat use system python (2.4), and as a result cannot find other installed applications under python 2.7. Python 2.7 is in ~/.bashrc and /.bash_profile files, so I do not have any problems running other applications, except when workers fail to work. When I run python ...../manage.py celery ( with all options) everything works like a charm.
Please let me know how I can force /init.d/function to run python2.7.
I have tried to implement #! /bin/sh python, but it does not work.

scripts in /etc/init.d are usually run as root on system startup. root's ~/.bashrc (that is /root/.bashrc) will look totally different from yours (e.g. /home/reza/.bashrc).
your shell will behave slightly differently if you are running it interactively or not.
hence there is no use in trying to run the python interpreter through /bin/sh, it only adds overhead.
what you want is to add a proper shebang that tells the system which interpreter to use for your script.
e.g.
#!/usr/bin/python2.7
will use the python2.7 binary installed in /usr/bin.
(so whenever you run /etc/init.d/foo.py the system really runs /usr/bin/python2.7 /etc/init.d/foo.py)

Related

Run Python script that affect parent shell (changes environment variables, runs other scripts, etc.)

Recently I need to affect bash shell that runs python script from python script itseft. I develop a Python utility package that add some additional functionallity to pip. One of the workflows in this package needs to active Virtualenv to work as planned. Here is the problem.
When you run something like:
os.system('/bin/bash ./venv/bin/activate')
Or:
subprocess.Popen(['/bin/bash', './venv/bin/activate')
It doesn't do anything to the shell when the script is executed. Basically because these commands are executed in isolated processes (I guess) and therefore does not affect bash process itself.
Question: how can you affect parent shell that execute python script from inside the script (add some environments, run other script, etc.)?
Thanks in advance.
how can you affect parent shell that execute python script from inside the script (add some environments, run other script, etc.)?
It is not possible to do that, unless your operating system is broken. Process isolation is one of the very basic concepts of an operating system.
Instead, resaerch what venv does and how it works and what activate script does and just add the proper directory to python module search path.

Scheduling Python Script with two versions of Python

I've got two versions of Python installed (2.7 and 3.5) via ArcGIS (Desktop 10.4 and Pro). I have multiple scripts that must run daily. The Python 2 scripts all run perfectly via Windows Task Scheduler, however, my Python 3 scripts simply refuse to run via the task scheduler.
I originally had them set to run from .bat files which works fine when operated manually.
#echo off
python3 \\Myserver\Pythonstuff\myscript.py
These .bat files will not operate via the task scheduler. Some things I've tried:
set python3.exe as the program/script and add in the script location as the argument
set cmd as the program/script and add python3 \Myserver\Pythonstuff\myscript.py as the argument
Nothing seems to work here. I need the other scripts to run in Python 2 and I can't mess everything up with them so I'd like to maintain their Python2/3 integrity between the scripts. Any ideas?

virtualenv & subprocess: How to get the correct path to a script

When someone installs my Python package, they can use the command mycmd (it's a console script added to the python bin/ directory). In turn, mycmd launches several other Python console scripts using subprocess:
subprocess.Process('celery arg1 arg2')
subprocess.Process('huey arg1 arg2')
...
(celery and huey commands are installed through my package's pip dependencies.)
This generally works fine, except in the situation where someone invokes mycmd directly without activating its virtualenv. For example, I am trying to use mycmd inside the process control system "circusd". See here where the circus.ini file invokes venv/bin/chaussette directly, without actually activating the venv. If I do this, I get the message celery: No such file or directory, I presume because the virtualenv is not activated and therefore those commands are not found on the path.
How can I ensure that when someone runs mycmd, the correct celery gets run, even if the virtualenv was not activated? (And should also work if the person is not using virtualenv at all, and cross-platform, etc.)
By the way, I am not using subprocess.Process directly, but rather using Honcho, which provides a layer around it.
I solved this simply by adding my virtualenv bin path to the PATH used by circus.

How to run python files without using terminal or gui?

Hello gyus i have started python and i want to know how can i excecute python file without using terminal.Just like the most games using (exe) file extension but for py files.I have tried py2exe but it doesn't show anything on the screen. I tried to make excecutable the py file with no luck. Please tell me how to excecute the and if there is an option whithout using a specific program for that.
My system is : Windows 7 / Ubuntu 12.04
Here, have a look in my blog, It explains how ca you do it in Ubuntu.
http://insidepython.wordpress.com/2012/08/04/hello-world-or-how-or-say-ni/
but basically:
Add this line to the beginning of your script
#!<location of your python interpreter>
To find out where your python interpreter is installed:
$ sudo find / -name "python"
After executing the previous, you should get the location of your python interpreter, then you need to set the environment variable, in my case python executable is located in /usr/bin/python
$ export PATH="$PATH:/usr/bin/python"
Then you need to set the file attributes to executable, you can look more into file attributes in Unix/Linux here http://en.wikipedia.org/wiki/Chmod
$ chmod +x shrubbery.py
And finally to execute your application
$ ./shrubbery.py
For Windows, there's also a python script called GUI2Exe that I have used in the past to create distributable versions of python scripts as apps. It's available at
http://code.google.com/p/gui2exe/ and is, in my opinion, very simple to use. Tutorials and whatnot are easy to find on Google.
It uses py2exe (or any other Python compiler library) to put together the script, but it doesn't require any syntactically annoying setup.py file.

Running python from the mac terminal

I have installed the new python release and would like to run .py files from the terminal.
How is this done from the terminal? I dont want to include the path in each command to run a .py file.
If you want to override the python command, you can set your PATH variable correctly, e.g. in your ~/.bash_profile:
export PATH=/path/to/python/:$PATH
That said, for managing different versions of components that are also provided by Mac OS X, I suggest to use a package manager such as Homebrew.
if you add a shebang at the start of the python file then you can run a python file by just its name from terminal
add #!/usr/bin/python
for mac(others add your respective path for python)
at the top of your python program and from your terminal you can run it just by filename(if it has executable permissions).
Have a look at the Python package under Applications. There is a shell script there called Update Shell Profile.command
Run this and it should set your path up properly.
Unless you mark you script as executable with chmod +x, you'll need to run python over it first. e.g. `python myscript.py'
I installed all of my python through macports, which has pros and cons. One of the benefits is that you don't have to worry about stuff like this, it just works. You can install python 2.6 and python 2.7 (and others), and then use the python_select utility to set up which python is run when you call "python blah.py"
Since you have installed a working python, the easiest way to run python files from the terminal is to cd your terminal to the directory where the file is located and then just type python my_code.py in the terminal.

Categories

Resources