I am new to python and am learning things by writing scripts.
I have tried the following and none of them seem to be working.
1) commands.getoutput('module load xxx')
2) subprocess.check_output(['module load', xxx'])
None of these change the environment as a side effect of the module call. Can someone tell me what is wrong?
I found a different approach to solve this problem.
I have written a shell script which load s the environment modules and i am calling that in the python script.
Something like this
import subprocess
subprocess.call(['./module_load.sh'])
and the script has something like this....
module load ***
This seems to be working.
Have to say.....its pretty simple in Perl. it provides the environment modules package to handle this.
Both commands create a subshell where the module is loaded - the problem is that this subshell is destroyed the moment Python function terminates
In case it helps anyone, I managed to do it by prefixing all commands that depend on the module with module load xxx &&. For example,
module load mpi/mpich && mpirun ./myprogram -n 4 -options
Not an elegant solution, but works for me. There's also this answer but I couldn't make it work in the system I have access to.
Related
I have a problem and hope someone can help me. I am currently trying to write a script for Termux or Termux:Task. My script currently looks like this:
#!/data/data/com.termux/files/usr/bin/bash
cd /./sdcard/www/public/
wp post list sleep 5
Every time I load the script I get the following error message:
/data/data/com.termux/files/usr/bin/wp: /usr/bin/env: bad interpreter: No such file or directory.
I've been looking for a solution to my problem for hours, unfortunately without success.
I am using an extension for Termux called "WordPress CLI". When I start termux and enter the commands individually, everything works. But as soon as I write the commands into a sh script and start it doesn't work anymore. :(
Can anyone help me?
Thanks a lot
This is simple error you can fix it by replacing !/data/data/com.termux/files/usr/bin/bash. With #!/data/data/com.termux/files/usr/bin/bash
Please tell if you get error again
Try with #!/usr/bin/env bash in the shebang line.
Termux-exec allows you to execute scripts with shebangs for traditional Unix file structures. So shebangs like #!/bin/sh and #!/usr/bin/env python should be able to run without termux-fix-shebang.
From https://wiki.termux.com/wiki/Termux-exec
According to doc:
Why do I keep getting a '/bin/sh bad interpreter' error?
This error is thrown due to access script interpreter at nonexistent
location.
Termux does not have common directories like /bin, /sbin, /usr/bin at
their standard place. There is an exception for certain devices where
/bin is a symbolic link to /system/bin, but that does not make a
difference.
Interpreters should be accessed at this directory only:
/data/data/com.termux/files/usr/bin
There are three ways to fix this:
Install termux-exec by using pkg install termux-exec. It won’t affect the current session, but after a restart should work without
any setup. Not needed if your Termux is up to date. If still not
working, try the next workaround.
Use command termux-fix-shebang to fix the shebang line of specified file.
Use termux-chroot from package proot to setup a chroot environment mimicking a normal Linux file system in Termux.
termux-fix-shebang my_script.py of second method work for me, which it modify the shebang(first line of my_script.py) from #!/usr/bin/env python to #!/data/data/com.termux/files/usr/bin/env python. Since /usr/bin/ is not exist in Android, that's why it throws the error /usr/bin/env: bad interpreter: No such file or directory. The other solution is run with python my_script.py, neither of my_script.py nor ./my_script.py.
In my test, termux-exec of the first method only work if I added correct shebang in main script(child OR child of child script no need) and ran command export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so.
And for the issue of this question, error shows /usr/bin/env in the middle with /data/data/com.termux/files/usr/bin/wp even though the shebang of script #!/data/data/com.termux/files/usr/bin/bash looks ok, it means that wp command (located at /data/data/com.termux/files/usr/bin/wp) used inside the script contains shebang #!/usr/bin/env wp and should modify it to #!/data/data/com.termux/files/usr/bin/env wp too. termux-exec of first method should fix this specific case too(already has correct shebang in main script).
I've a project in which a python script must get the path to some of its imports from an environment variable. I'm handling this with
sys.path.insert(0, os.environ["PYDIR"])
which works fine when I run the application.
Pylint (in my Vim editor) is flagging import errors on all the modules I import from "PYDIR". I hate to disable the error message in the module since it's usually useful. OTOH, I also hate the visual clutter in my editor.
I suspect there's no good solution since Pylint doesn't execute the code but thought I'd ask anyway.
EDIT: See my below comment on difference between this question and PyLint "Unable to import" error - how to set PYTHONPATH?
For my particular case, the following seems to work satisfactorily. (I'm developing on OS X, I start a customized Terminal shell when working on this project, and my editor is 'mvim' (MacVim))
Add to the Terminal startup shell command list:
source .myprojectshellenv;
where .myprojectshellenv contains:
export PYDIR=path-to-myproject-python-modules
alias pmvim="env PYTHONPATH=$PYDIR mvim"
Then all I need remember is to edit the files with pmvim instead of mvim. (If I forget the error indicators from pylint (via syntastic) make it immediately obvious)
I have just started looking & learning Python (on Ubuntu 14.04) using the website: http://learnpythonthehardway.org/book/ and http://www.codecademy.com/
Yesterday, I worked for an hour on a few scripts namely 1-4 and named them as ex1.py etc and they executed fine. Today, I have come back to carry on with a few more exercises and tested my first exercises only to find now I get "SyntaxError: invalid syntax" when attempting "python ex1.py" with or without .py I have tried "#!/usr/bin/python" in any script header also. The path/Dir I have been using to test my Python scripts in is simply 'Python' within my Home Dir. I checked the actual file permissions but that appears to make no difference.
I am not too sure if it's an OS setup issue; Python issue; or simply me. Python seems great, so any ideas on what I'm doing wrong? What has changed since yesterday to my python.py script files?
The problem is that you are in the Python interpreter, and not the Linux terminal. This is indicated both by the error messages you saw and by the >>> prompt.
Exit the interpreter by holding down control and pressing the D key. You will then be back in the Linux terminal and running a script will work the way you expect it to.
I'm using Gramps 3.3.1 under Ubuntu 12.04 and trying to in Django Export/Import. The install is okay, but when I look at the installed addons, it says that it failed, with reason: "No module named web.settings".
I cann't for the life of me figure out how to fix this. I'm assuming that it's this line in the python script:
import web.settings as default_settings
Anyone have any ideas? I'm trying to get this data out and into a MySQL database, but I can't seem to.
Thanks!
When you run into an error where Python tells you "No module named blah" it really means it can't find "blah".
I would check your pythonpath and the filesystem permissions on the stuff in your path.
Edit
Your issue seems to be a known issue: http://www.gramps-project.org/bugs/view.php?id=5464
It's caused by not running Gramps from source, near as I can tell. It doesn't look like a Python Path issue, so much as the Gramps Django devs making assumptions where your Django settings would be (but I could be wrong).
As for Python Path, there's plenty of resources describing how to. The simplest way to check your Python path is to run echo $PYTHONPATH in a terminal. If you don't see it there, Python might not find it.
In Fabric, when I try to use any alias' or functions from my .bash_profile file, they are not recognized. For instance my .bash_profile contains alias c='workon django-canada', so when I type c in iTerm or Terminal, workon django-canada is executed.
My fabfile.py contains
def test():
local('c')
But when I try fab test it throws this at me:
[localhost] local: c
/bin/sh: c: command not found
Fatal error: local() encountered an error (return code 127) while executing 'c'
Aborting.
Other Fabric functions work fine. Do I have to specify my bash profile somewhere in fabric?
EDIT - As it turns out, this was fixed in Fabric 1.4.4. From the changelog:
[Feature] #725: Updated local to allow override of which local shell is used. Thanks to Mustafa Khattab.
So the original question would be fixed like this:
def test():
local('c', shell='/bin/bash')
I've left my original answer below, which only relates to Fabric version < 1.4.4.
Because local doesn't use bash. You can see it clearly in your output
/bin/sh: c: command not found
See? It's using /bin/sh instead of /bin/bash. This is because Fabric's local command behaves a little differently internally than run. The local command is essentially a wrapper around the subprocess.Popen python class.
http://docs.python.org/library/subprocess.html#popen-constuctor
And here's your problem. Popen defaults to /bin/sh. It's possible to specify a different shell if you are calling the Popen constructor yourself, but you're using it through Fabric. And unfortunately for you, Fabric gives you no means to pass in a shell, like /bin/bash.
Sorry that doesn't offer you a solution, but it should answer your question.
EDIT
Here is the code in question, pulled directly from fabric's local function defined in the operations.py file:
p = subprocess.Popen(cmd_arg, shell=True, stdout=out_stream,
stderr=err_stream)
(stdout, stderr) = p.communicate()
As you can see, it does NOT pass in anything for the executable keyword. This causes it to use the default, which is /bin/sh. If it used bash, it'd look like this:
p = subprocess.Popen(cmd_arg, shell=True, stdout=out_stream,
stderr=err_stream, executable="/bin/bash")
(stdout, stderr) = p.communicate()
But it doesn't. Which is why they say the following in the documentation for local:
local is simply a convenience wrapper around the use of the builtin Python subprocess module with shell=True activated. If you need to do anything special, consider using the subprocess module directly.
One workaround is simply to wrap whatever command you have around a bash command:
#task
def do_something_local():
local("/bin/bash -l -c 'run my command'")
If you need to do a lot of these, consider creating a custom context manager.
It looks like you're trying to use virtualenvwrapper locally. You'll need to make your local command string look like this:
local("/bin/bash -l -c 'workon django-canada && python manage.py runserver'")
Here's an example by yours truly that does that for you in a context manager.