How to run gimp like it's a normal python file? - python

So example I have got an effects.py file and to run on the command prompt, I am doing
gimp-console-2.10.exe -idf --batch-interpreter python-fu-eval -b "import effects; effects.data()" and this works.
However I don't really want to run it in this way. Is there a way I could run it like python effects.py on the command prompt instead?
#!/usr/bin/env python
from gimpfu import *
def data():
pass

If you want to avoid the import, you can install your python code as a regular plugin, and then you should be able to skip the import and call your plugin directly:
gimp-console-2.10.exe -idf --batch-interpreter python-fu-eval -b "pdb.python_fu_your_registration_atom(...)"
(not tested)

Related

Execute commands from python prompt using os.system

I am trying to execute a few commands using python shell to import a module ABC. For that I have created a python file test.py having the following lines:
import os
os.system('python')
os.system('import ABC')
Now I am trying to execute that file as follows - python test.py. Here, it runs till the 2nd line i.e. os.system('python') properly. Then when it enters into the python prompt and there it remains stuck as it is not command prompt any more but python prompt. So how to execute the next command of importing the module ABC on the python prompt?
Command line and environment (Python Documentation)
python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]
In your terminal, when you run python followed by the name of a script (test.py), you are executing the contents of the script. Within that script, when you call 'python' using os.system('python'), you are starting a new interactive session, similar to if you were to just call 'python' from your terminal.
import os
os.system('python -c "import ABC"')
You can use the -c argument with python.
For example,
import os
os.system('python -c "import ABC"')
It sounds like what you need is to put the commands you require in a Python script, e.g. my_imports.py:
import ABC
import my_module
And run it from interactive Python with something like:
exec(open('my_imports.py').read())
That will execute the code in the script in the current context, making the imports available to you on the Python CLI.

python: how to run a program with a command line call (that takes a user's keystroke as input) from within another program?

I can run one program by typing: python enable_robot.py -e in the command line, but I want to run it from within another program.
In the other program, I imported subprocess and had subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e']), but I get an error message saying something about a callback.
If I comment out this line, the rest of my program works perfectly fine.
Any suggestions on how I could change this line to get my code to work or if I shouldn't be using subprocess at all?
If enable_robot.py requires user input, probably it wasn't meant to run from another python script. you might want to import it as a module: import enable_robot and run the functions you want to use from there.
If you want to stick to the subprocess, you can pass input with communicate:
p = subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e'])
p.communicate(input=b'whatever string\nnext line')
communicate documentation, example.
Your program enable_robot.py should meet the following requirements:
The first line is a path indicating what program is used to interpret
the script. In this case, it is the python path.
Your script should be executable
A very simple example. We have two python scripts: called.py and caller.py
Usage: caller.py will execute called.py using subprocess.Popen()
File /tmp/called.py
#!/usr/bin/python
print("OK")
File /tmp/caller.py
#!/usr/bin/python
import subprocess
proc = subprocess.Popen(['/tmp/called.py'])
Make both executable:
chmod +x /tmp/caller.py
chmod +x /tmp/called.py
caller.py output:
$ /tmp/caller.py
$ OK

call python module functions from command line

I would like to know if there is a way to call module functions from the command line. My problem is that I have a file package called find_mutations. This requires that the input files are sorted in a specific way, so i made a function to do so. I would like the user to be able to have the option to run the code normally:
$ python -m find_mutations -h
or run the specific sort_files function:
$ python -m find_mutations.sort_files -h
Is something like this possible? If so, does the sort_files function need to be in its own strip? Do I need to add something to my __init__.py script? The package install properly and runs fine, I would just like to add this.
You could add flags to run the function. The following example is not a good way to work with command arguments, but demonstrates the idea.
import sys
if __name__ == "__main__":
if '-s' in sys.argv:
sort_files()
Add a flag to run the specific function, in this case -s to sort the files.

How to make IDLE automatically import modules at start?

Often when I am using the IDLE shell I import the pickle module. Is it possible to make it automatically import pickle when I start it?
You can use the -c or -r argument:
From idle -h:
-c cmd run the command in a shell, or
-r file run script from file
For example:
idle -c 'import pickle, sys'
Or:
idle -r ~/my_startup.py
Where my_startup.py might contain:
import pickle, sys
You can either create a shell alias to always use this, or create a separate script; the procedure for this differs depending on your OS and shell.

Changing dir to execute program in linux

I am trying to execute a program from a directory
import os
os.chdir("/home/user/a/b")
with cd("/home/user/a/b"):
run ("./program")
i get cd is not defined...
any help appreciated cheers
I'm not sure what instructions you're following to get what you showed. There is no built-in function called cd or run in Python.
You can call a program in a specific directory using the subprocess module:
import subprocess
subprocess.call("./program", cwd="/home/user/a/b")
The cwd argument causes the call function to automatically switch to that directory before starting the program named in the first argument.
It looks like you are trying to use functionalities of fabric. Make sure fabric is installed, and cd and run are imported from fabric. Something like,
from fabric.context_managers import cd
from fabric.operations import run
import os
os.chdir("/home/user/a/b")
with cd("/home/user/a/b"):
run ("./program")
Save your file as fabfile.py,and from the same directory run it as:
fab -H localhost
For more information on the fabric, checkout: fabric

Categories

Resources