Run a python script from bamboo - python

I'm trying to run a python script from bamboo. I created a script task and wrote inline "python myFile.py". Should I be listing the full path for python?
I changed the working directory to the location of myFile.py so that is not a problem. Is there anything else I need to do within the configuration plan to properly run this script? It isn't running but I know it should be running because the script works fine from terminal on my local machine. Thanks

I run a lot of python tasks from bamboo, so it is possible. Using the Script task is generally painless...
You should be able to use your script task to run the commands directly and have stdout written to the logs. Since this is true, you can run:
'which python' -- Output the path of which python that is being ran.
'pip list' -- Output a list of which modules are installed with pip.
You should verify that the output from the above commands matches the output when ran from the server. I'm guessing they won't match up and once that is addressed, everything will work fine.
If not, comment back and we can look at a few other things.
For the future, there are a handful of different ways you can package things with python which could assist with this problem (e.g. automatically installing missing modules, etc).

You can also use the Script Task directly with an inline Python script to run your myFile.py:
/usr/bin/python <<EOF
print "Hello, World!"
EOF
Check this page for a more complex example:
https://www.langhornweb.com/display/BAT/Run+Python+script+as+a+Bamboo+task?desktop=true&macroName=seo-metadata

Related

Pipenv and ModuleNotFoundError

I have spent hours looking into this issue without any success.
I've looked at various SO discussions and none seem to solve my problem so out of pure frustration here is my question...
I'm trying to launch a script within a windows batch file. The problem is that when I do the script fails because it can not find some of the modules used.
After various attempts I have found that the batch file aspect, at this stage, seems to be irrelevant.
So, ignoring batch files for a minute, If I run the script like this
pipenv run python myscript.py
It works. If I run the following it doesnt
path-to-env\Scripts\activate
python myscript.py
It returns an error ModuleNotFoundError: No module named 'xxx'
It activates the venv OK, but something is not right as it cant find code used in script
Within my IDE (Visual Code) everything works OK
I do have quite a complicated directory structure but given that both the IDE and "pipenv run python myscript.py" work as expected it must be due to something else.
Any ideas or pointers on where I need to be looking? I'm afraid my understanding of pipenv isnt up to solving this ;)
EDIT
In my attempts to solve this I had added the line PYTHONPATH=. to my .env file. This seems to be responsible for allowing this line to work:
pipenv run python myscript.py
If I remove it, then the above ALSO generates the ModuleNotFoundError
OK so after trying lots of various combinations I did finally manage to get this to work.
Although I have no idea why this solution works and others didnt..
It requires two batch files.
One to launch the python script which will contain a line like this
python myscript.py
And another to create the env via pipenv and then call the first batch file
It will have a line like this
pipenv run \path\to\first\batchfile.bat
This combination works and can be successfully called from the Windows Task Scheduler

under what application does dev_appserver.py runs?

I'm not able to understand how
google-cloud-sdk/bin/dev_appserver.py helloworld runs automatically.
I mean to ask that it should run under python command as it is a python file.
is there something added to path??
Yes, dev_appserver.py is an executable python script. If you launch it by itself it will be executed under whichever python is the default one in your environment, see its 1st line:
#!/usr/bin/env python
If you want you can also execute it with a specific python version (it has to be a python 2.7 one, though, the only one supported by GAE standard environment):
/specific_path/python /path_to/google-cloud-sdk/bin/dev_appserver.py helloworld

How would I allow others to use my script from anywhere in the shell without having to type out the file extension?

Excuse the awkward question wording.
I've made a script. I would like for others to download it from github, and run it by typing programName argument1 argument2, similar to any other popular app used through the terminal such as Jupyter or even opening Atom/Sublime/etc. (ex:jupyter notebook, atom .). However, unlike Jupyter or sublime, my script isn't launching another app, it's a small app meant to be used in the shell.
Currently, to use my script, one must type into the command line python programName.py arg1 etc from within the file's directory.
How do I allow others to dl it and use it from anywhere (not having to be within the directory), without having to type out the whole python programName.py part, and only having to type programName arg1?
This blog post explains step by step how to create a distribution that you can install and it would turn into an executable.
You can refer to this github repo for a sample application.
The full documentation of setuptools is available here.
In general, you should configure your setup.py in order to use the command in the entry-point option:
setup(
name = "your_app_name",
packages = ["package_name"],
entry_points = {
"console_scripts": ['cmd_name = package_name.package_name:main']
},
....
)
This solution would work on every OS where you have installed python.
Your script may need to have an interpreter, "shebang", besides being "reachable" by the $PATH
#!interpreter [optional-arg]
For example, you could have something like
#!/usr/bin/env python
or to force a specific version
#!/usr/local/bin/python2.7
Next, your script needs to be available within the $PATH, check this answer that covers that part: https://unix.stackexchange.com/q/29608/53084
You can simply add your script to PATH variable in order to launch it from anywhere.
In Linux distros, you can simply do it by using a bash command PATH=$PATH:/path/to/your/script.
Make sure you don't have the space around the "=" operator.
Now, the second thing is you don't want your script to be named as pythonProgram.py.You can simply remove the extension .py from PythonProgram.py by adding a single line to the starting of your script.
Open up your script and at the very begining type #!/usr/bin/python.This should be the first line of your code.This line is called shebang and is used to tell the bash which interpreter to be used for compiling the script.
If everything went right, you will be able to run your script as pythonProgram arg1.
In addition to mabe02: Python is a scripting language and usually not compiled, which means you will need an interpreter to run your program.
Programms made in C f.e. can be run on its own because they were compiled into machine code by a compiler.
An interpreter is similar to a compiler as it reads your script and interprets it at runntime. That is why you need to write python before your programm, to tell your computer to interpret your script on runntime, using python. This doesn't mean that there are no possibilities to compile python as can be seen in the other answer and in this link Can a python program be run on a computer without Python? What about C/C++? (py2exe and py2app).

Python - running a script as a service with a name other than 'python'

I have a set of python scripts which I run as a daemon services. These all work great, but when all the scripts are running and I use top -u <USER>, I see all my scripts running as python.
I would really like to know which script is running under which process id. So is there any way to execute a python script as a different process name?
I'm stuck here, and I'm not ever sure what terms to Google. :-)
Note: I'm using Ubuntu Linux. Not sure if the OS matters or not.
Try using setproctitle. It should work fine on Linux.
Don't have a linux system here to test this on appropriately, but if the above doesn't work, you should be able to use the same trick they use for things like gzip etc.
The script has to tell what to run it at the top like this:
#!/usr/local/bin/python
Use a softlink like this:
ln -s /usr/local/bin/python ~/bin/myutil
Then just change your script to
#!~/bin/myutil
and it should show up that way instead. You may need to use a hard link instead of a soft link.
Launching a python script using the python script itself (and file associations and/or shell magic) is not very portable, but you can use similar methods on nearly any OS.
The easiest way to get this is using she bang. The first line of your python script should be:
#!/usr/bin/python
or
#!/usr/bin/python3
depending upon whether you use python or python3
and then assign executable permissions to the script as follows:
chmod +x <scriptname>
and then run the script as
./scriptname
this will show up as scriptname in top.

Run a Python script from PackageMaker in OSX

I'm having a difficult time finding any decent documentation for packaging on OSX. My package needs to run a Python script that will do some launchd magic right after the app is installed. I tried using -s scripts with packagemaker and put my script in the directory scripts, but I had no luck. Is there something I'm missing? More importantly, where is all the packagemaker documentation? How can I get my Python script to run? The only thing I've seen is this, which is way outdated and doesn't cover the command line version.
I don't want to use the GUI; it makes my life too difficult.
I've resolved this issue.
The script needs to be named postflight and have a proper shebang line.
The script needs to exit with specific status codes (0 for success, which is the default, I believe).

Categories