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.
Related
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)
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.
I'm trying to run sh-script from python file
my_script.sh
#!/usr/bin/python
rm category.xml
python file
import subprocess
subprocess.call(["../my_script.sh"])
And I get
File "../my_scrypt.sh", line 3
rm category.xml
^
SyntaxError: invalid syntax
How to fix this?
You used a shebang line of #!/usr/bin/python on a file that isn't Python. Change the shebang line.
Better yet, don't call out to shell scripts when you can call Python functions to do the same thing:
import os
os.remove("category.xml")
Look at your shell code. You using python interpreter #!/usr/bin/python and feeding it with bash commands rm category.xml.
Fixed shell script:
#!/bin/bash
rm category.xml
if you're using python 2x
use commands module:
import commands
print commands.getoutput('sh my_script.sh')
if using python 3x
use subprocess module:
import subprocess
print(subprocess.getoutput('sh my_script.sh'))
Try this,
my_script.sh
#!/usr/bin/sh
rm category.xml
Trivial approach:
>>> import subprocess
>>> subprocess.call(['./my_script.sh'])
>>>
So here's an example of the terminal line I'm trying to run after importing the OS into a Python Script of mine:
user$ echo variable | thecommand
Even though OS imports have been working for me lately, the fact that the variable is in the MIDDLE of the imported OS command is not allowing my code to run:
#! /bin/python
import os
variable = 'thevariable'
os.system ("echo "+variable +" | thecommand")
the above is what I have tried in a few different syntax's with no success. Is there a way to accomplish what I'm looking to do using the os.system method?
Don't use os.system(). it is deprecated.
Instead try
import subprocess
variable = 'thevariable'
subprocess.call("echo "+variable +" | thecommand", shell=True)
the shell=True means that the command will be run in a bash process so that echo and the pipe would work.
I am amending an existing script in which I want to check the set of libraries used in an executable with the shared libraries called at the run time. I have the list of libraries which I need to compare with the shared libraries. For getting shared libraries I am trying to get LD_LIBRARY_PATH by giving below code but I had no luck. I tried checking the variable on command line by giving
echo $LD_LIBRARY_PATH
and it returned /opt/cray/csa/3.0.0-1_2.0501.47112.1.91.ari/lib64:/opt/cray/job/1.5.5-0.1_2.0501.48066.2.43.ari/lib64
the things that I have already tried are (this is a python script)
#! /usr/bin/python -E
import os
ld_lib_path = os.environ.get('LD_LIBRARY_PATH')
#ld_lib_path = os.environ["LD_LIBRARY_PATH"]
I think you are just missing a print in your script? This works for me from the command line:
python -c 'import os; temp=os.environ.get("LD_LIBRARY_PATH"); print temp'
script:
#! /usr/bin/python -E
import os
ld_lib_path = os.environ.get('LD_LIBRARY_PATH')
print ld_lib_path