I run python in my Android Terminal and want to run a .py file with:
python /sdcard/myScript.py
The problem is that python is called in my Android enviroment indirect with a shell in my /system/bin/ path (to get it direct accessable via Terminal emulator).
My exact question, how the title tells how to pass parameter through multiple Shell scripts to Python?
My direct called file "python" in /System/bin/ contains only a redirection like:
sh data/data/com.hipipal.qpyplus/files/bin/qpython-android5.sh
and so on to call python binary.
Edit:
I simply add the $1 parameter after every shell, Python is called through like:
sh data/data/com.hipipal.qpyplus/files/bin/qpython-android5.sh $1
so is possible to call
python /sdcard/myScript.py arg1
and in myScript.py as usual fetch with sys.argv
thanks
I don't have experience in Android programming, so I can only give a general recommendation:
Of course the naive solution would be to explicitly pass the arguments from script to script, but I guess you can't or don't want to modify the scripts in between, otherwise you would not have asked.
Another approach, which I sometimes use, is to define an environment variable in the outermost scripts, stuff all my parameters into it, and parse it from Python.
Finally, you could write a "configuration file" from the outermost script, and read it from your Python program. If you create this file in Python syntax, you even spare yourself from parsing the code.
I have similar problem. Runing my script from Python console
/storage/emulator/0/Download/.last_tmp.py -s && exit
I am getting "Permission denied". No matter if i am calling last_tmp or edited script itself.
Is there perhaps any way to pass the params in editor?
Related
I need to start a script in a termux environment on my android tablet from an other android app. I think it should be possible in two ways.
setting up some kind of startup script (like the ~/.bashrc in the bash shell) in termunx and starting termux from the other app
calling termux from the other app with the script name as parameter, so that termux executes that script immediately.
I don't know how to do either of these possibilities.
Does anybody know how to accomplish it, maybe with a third method, I didn't think of so far?
I didn't see the wood for the trees. The answer is exactly my first suggested way. I didn't realize, that bash is the default shell in termux. So just one of .bashrc .profile .bash_profile do the job.
please can someone help i have not done this for a while. I am trying to execute a monkeyrunner python script. In my c:\Android\sdk\tools\bin directory i run the following: monkeyrunner monkeyRun.py. Nothing happen only jython command line starts
Passing the script file to the monkeyrunner is the right thing to do, see https://developer.android.com/studio/test/monkeyrunner/index.html.
If you provide a filename as an argument, the monkeyrunner command runs the file's contents as a Python program; otherwise, it starts an interactive session.
How to create and activate conda (a virtual environment) from a bash script?
I've read a lot of stack overflow posts already None of them really make any sense to me, given that I'm a beginner with bash. Also, most of them pertain to virtualenv, and not conda, which adds to the confusion.
I don't really understand how source or exec works, or if I even need to use it for this purpose.
All I'm trying to do is create a conda virtual environment from inside a bash script, and then activate it. Then run more commands via the bash script on the newly activated "environment".
Instead, what is happening when I run the script below, is that the environment is created, but it's not activated. It's also not helpful that the terminal asks for a prompt to proceed with the creation of the virtual environment (human input required not good).
Here's the script:
#!/bin/bash
dirname=$1
conda create -n $1 python=2.7
source activate $1
Terminal shows:
Jills-MBP:Desktop jillr$ bash site_builder.sh blah
Fetching package metadata: ....
Solving package specifications: ..........
Package plan for installation in environment /Users/jillr/anaconda/envs/blah:
The following NEW packages will be INSTALLED:
openssl: 1.0.2l-0
pip: 9.0.1-py27_1
python: 2.7.13-0
readline: 6.2-2
setuptools: 27.2.0-py27_0
sqlite: 3.13.0-0
tk: 8.5.18-0
wheel: 0.29.0-py27_0
zlib: 1.2.8-3
Proceed ([y]/n)?
The solution was as stupid as calling the entire script with "source" instead of "bash". UHHHHHHHHGGGG!
Instead of
bash site_builder.sh blah
Use
source site_builder.sh blah
If someone actually explains why this solves it, that would be fantastical, because I still don't understand what "source" is doing.
https://superuser.com/questions/46139/what-does-source-do says, "source is a bash shell built-in command that executes the content of the file passed as argument, in the current shell."
As opposed to what other shell?
"source script reads and executes commands from filename in the current shell environment"
What other shell would they be executed in?
What shell is "bash site_builder.sh blah" opposed to "source site_builder.sh blah" ??
I'm getting a permission denied error when my QPython android script tries to launch another qpython script.
I'm assuming that this can be done - right?
I've tried subprocess.call, whic seems like the right thing to do.
But, the script doesn't run, and the log shows the permission denied error.
The obvious thing to do is look at the permissions of the sub script, but I don't know how to do that on my Android phone.
Any comments/suggestion appreciated.
Thanks.
First, how to open a command prompt(bash)
Actually qpython = terminal emulator + Python code editor + Python interpreter.
So of course you can use the console! Why you can't enter bash is because every time you click the console icon in qpython it runs python. Just type something which will cause the interpreter error like "I want bash!" then run it, interpreter will break then go back to bash.
Second, how qpython run your script
It pass your python script to a shell script(qpython.sh), then run the your script by a python interpreter.
Third, about the permissions
Why permission denied? You can't run a python script directly because it's not marked as executable. But you can pass the script's path to python interpreter to run it.
Finally, so how to call a python script by a python script in qpython?
#-*-coding:utf8;-*-
#qpy:2
#qpy:console
import subprocess
print("I am calling myself!")
pysh="/data/data/org.qpython.qpy/files/bin/qpython-android5.sh"
#if U R using android 5
#else
pysh="/data/data/org.qpython.qpy/files/bin/qpython.sh"
callpy=__file__
#the script path you want to run
subprocess.call([pysh,callpy])
Hope this help you!
(I can't add image by myself because I don't have that much reputation, I need someone else to change my image link to image. So if you like my answer, please vote me. Thanks.)
Once you know where the script is, just move to that folder and then use the following command to list the files:
ls -l
the command outputs all the files together with permission (first column).
Now, to change the permission to "execution" you can use:
chmod 755 [your-script-name]
I've never used python before, but I have to in order to enable push notifications on my android app. When I type
python D:\Project\UAirship\clientauth.py
I get a syntax error with an error pointing to the drive letter. I've read support articles on urban airships website, and this is the way they execute this script so I'm not sure what I'm doing wrong. Thanks for your help.
You're supposed to type that in your shell/command interpreter, not the Python REPL.
Check to make sure you've added Python to your path. At the C:> prompt type
python
and see what happens. If you get the Python shell, great. Else do what the faq says.
edit: just fired up a windows box to check, and the D: shouldn't be a problem, never mind the thought that was previously occupying this space.
It seems likely that ignacio is right in his answer: you're typing a command-prompt command into the python shell. Your cursor should be flashing after something that looks like this
C:\>
if it looks like this
>>>
type exit() to get out of the python shell and try again.