Run python script with custom unix command? [duplicate] - python

To be clear, not just "run python scripts"
But to make my python script a "executable" or "callable" program
that can be used in other programming languages or platforms.
More like a API maybe.
The thing is I implemented several algorithms in java and
they're supported by numpy and spipy, but others want to
call my python program in their java program.
Then the numpy and spipy are problems. They can't be in java and
jython...
Is there a solution that I can make this an executable program that others don't need the environment but just to run the program with several parameters accepted?

If you just need to make the python programs executable, then you can add a line to the top like this:
#!/usr/bin/python
Replace /usr/bin/python with the filepath to python, which can be obtained by typing
which python
into the terminal, assuming you're using a unix-based system.
You can then tell the operating system that the program is executable with
chmod +x nameofprogram
If the java programs require something more complicated than just being able to run the python parts as executables, then you'll probably need to provide more information for anyone to be able to help you.

Related

How to create an executable on Mac to run python scripts (bash help)

Sorry if this is an easy to answer question, but I have been stressing myself out all day over this simple problem. I have never used a Macbook before, and am unfamiliar with the inner-workings. I wrote a series of six python scripts that are meant to be run in series, and have easily been accomplishing this with a batch files on my PC. However, I have been developing this program for a Mac user, and have no clue how to accomplish the same thing.
I have successfully managed to get python installed as well as all of the necessary packages, and the scripts can be run one-by-one, so the infrastructure is there.
On windows, I have been accomplishing this with the following batch script:
#echo off
python outputnotion.py
python addData.py
python listAppender.py
python inputgsheets.py
ECHO Timing out for 30 seconds to allow Google Sheets to compute values
timeout /t 30 /nobreak
python outputgsheets.py
python inputnotion.py
pause
I have no idea how to replicate this on mac, or if it's even possible. The person who will be using this code is not as familiar with python or running the scripts, so the simpler the solution the better.
Thank you so much, as I have been scratching my head all day over this seemingly simple issue.
Have you heard of the makefile? It works on UNIX-based systems, such as the MacOS. I use them mostly to store snippets of bash scripts I use frequently.
Make a file named Makefile
Within Makefile, write something like this:
scripts:
python outputnotion.py;
python addData.py;
python listAppender.py;
python inputgsheets.py;
## This is a comment
## Sleep for 30 seconds
sleep 30;
#echo Timing out for 30 seconds to allow Google Sheets to compute values;
python outputgsheets.py;
python inputnotion.py;
## Invoke a pause
read -p "Press [Enter] key to continue...";
Now, using your bash terminal in the directory where Makefile resides, type the following to execute your scripts:
$ make scripts
A shell script just runs builtin commands and external programs. You certainly can write a string of invocations to python scripts, not just compiled programs. The shell is really a full programming language, with variables, control structures and all. A bit quirky, but I've seen largeish scripts doing complex tasks (not that I'd recommend doing so, there are better tools).

How to use Unix commands in python that works on windows operating system

Is it possible to use sed and awk kind of commands in python that works on windows operating system? For Unix operating system we can import os package and use os.system to perform any unix operations. I want the same kind of operations to be performed in Windows operating system. Can anyone please let me know if there is any such package that can be used in python.
P.S. I want to perform such actions as part of .py script instead of having them executed in command lines as in IPython.
As an example, I want this kind of CODE to be executed in Windows operating system.
import os
os.system("sed -ie 's/ow/aagh/g' ~/temp.txt")
Yes, this works. However, you will need to install the unix programs you want to run on windows.
For example, you can find sed here: http://gnuwin32.sourceforge.net/packages/sed.htm
To have most of the common unix commands available on windows, have a look here:
https://www.cygwin.com/
As stated in the other answer: yes, you can do that if you want force your users (or yourself) to install the appropriate linux commands also on windows.
However if you really want to make it a portable script you'll have to use the appropriate python modules in order to achieve the same goal. See for instance this question and its answers python equivalent to sed

How to turn Python program into executable file

I apologize for such a basic question. I've done some research online and still cannot figure out for the life of me how to turn a python folder into something like an actual app I can open in OS X. I am using Mac OS X, Terminal and Coderunner for my Python project.
Here are a few options:
Platypus is not Python-specific. It lets you wrap a simple GUI around a command line tool.
py2app is Python-specific and a good choice if you have a GUI, or need to run in the background.
PyInstaller is similar to py2app but cross-platform; I've never used it, so I don't know how well it works.
The right choice depends on what your program does; who is the expected audience — do you need to redistribute it, if so how, and so forth. If you want to make the application entirely self-contained — not dependent on anything else beyond the OS — then things get more complicated (though certainly not insoluble; there are several commercial Mac desktop apps written in Python.)
Typically you would make the script executable by putting
#!/usr/bin/env python
as the first line, and then in a terminal window typing
chmod u+x myscript.py
You might also want to put it in a special scripts folder and then add that to your PATH by editing .bash_profile. (I am putting a lot of buzz-words here to help you find the tutorials explaining how these things work.)
You can wrap your script into an Automator object if you want to try running it that way, but in the long run you will be better off getting comfortable working in a terminal window.
It would also help to know what your app does: Process files, generate a GUI, do a calculation, etc...

How can I make my program utilize tab completion?

I've noticed that some programs (e.g. hg) allow the user to tab-complete specific parts of the command. For example, if, in an hg repository working directory, I type:
hg qpush --move b8<TAB>
It will try to complete the command with any mercurial patches in my patch queue that start with "b8".
What I'd like to do is imitate this behavior in my program. That is, I have a series of commands that depend on files within a certain directory, and I'd like to be able to provide tab completion in the shell. Is there an API for providing this on Ubuntu Linux (preferably using python, as that's what my script is written in)?
To do this, you need to write tab-completion modules for your shell. The default shell in most Linux distributions is bash, so you should write a completion script (typically a shell script). Once you've written your script, add it to /etc/bash_completion.d/. This should be distributed with your program (for Linux distributions, included in the package).
Debian Administration has a guide for writing your completion scripts. For using completion on a Mac, see https://trac.macports.org/wiki/howto/bash-completion.
For examples of completion files, take a look at the bash-completion project from Debian (also on Github). See also https://unix.stackexchange.com/questions/4738/an-easy-bash-completion-tutorial.
If you use zsh, hack.augusto linked to the documentation for writing completions.
This is what the readline module is for.
Actually, readline is a common C library so it has bindings in many languages. And I can say, I've had tons of fun with it.
Enjoy B)
You might want to try the zsh shell, it has a great completion system with support for tons of applications.
The completion system is written with the shell language, but if you really want to use python you can run the interpreter from inside your completion function. The down side it that if you want to write completion for your own software, you will need to do some reading (user manual and the manpage for instance).
Take a look at the source of the 'cmd' module in the Python library. It supports command completion.

python programming query

I'm new to Python programming.
My question is where to write python programs in linux--
in terminal or
any writing pad like gedit etc
Should we write one program again and again for running or we should call the program and run it.
After you install Python (it's installed by default on most Linux-es), you have two options:
Terminal. Just type python <ENTER> and you will be taken to the Python interpreter interactive prompt. For anything but the most basic stuff, however, you may want to intall IPython - an alternative interactive prompt that's much better than the default one.
In a file. Save your code into a .py file and run it with python myfile.py
But first and foremost, start by learning Python - the official tutorial is a great place to start. Among other useful information, its chapter 2 discusses how to use the Python interpreter for beginners.
Stackoverflow also has a lot of great resources for learning Python. This question, for example, and many others.
If you are learning, or you are evaluating expressions, you could run Python in terminal, or use IDLE.
But if you are writing large chunks of code, then you should consider using an IDE.
You could either use Geany, or use Eclipse with PyDev. I prefer Eclipse myself.
For running, you can run it using the command python program.py, or just add the line
#!/bin/python
to the beginning of your program, grant it execution permission using chmod, and run it with ./program.py command.

Categories

Resources