How to run python script within the python interpreter in Bash - python

I am using python3.5 to learn how to scrape data from website. And I realize IDLE is slow when the input explodes (like when I using .text to check the contents of the web). So I use Bash to test my scraper.py script.
After I enter python in Bash:
154-76:~ FDSM_lhn$ python3.5
It's hard for me to open a .py file. The only way I know how to do that is:
import scraper.py
which is not convenient because the object I create isn't in that environment. During the test I need to check something in it from time to time.
Can anyone can help me fix this?

If you have a file called scraper.py, you should be able to execute it via python -i scraper.py. This will leave it interactive.

Related

Create python script to run terminal command with source

Long story short, I'm trying to figure out a way to turn these command lines into a script or function in python that can be called by another python application.
These are the command lines in Linux:
source tflite1-env/bin/activate
python3 stream.py --modeldir=TFLite_model
At first I was like this will be easy its just launching the application in python - but I'm not really sure how to deal with the source part. I thought it was just accessing the directory, but its doing something with the .csh file...
I tried a very convoluted method by creating a .sh file like so:
#!/bin/bash
cd ..
cd tflite1
source tflite1-env/bin/activate
python3 stream.py --modeldir=TFLite_model
and then making a function for my main program to call said file:
import os
def startCamera():
os.system('sh script.sh')
With this method I get an error about the source not being found.
I think the issue is I'm basically trying to call too many separate processes that are terminating each other or something? There's got to be a better way. Any suggestions would be greatly appreciated!
I changed the function to:
import subprocess
def startTheCamera()
subprocess.call("/home/pi/Desktop/launchCamera.sh")
I used subprocess instead of os.system and included the full file path and it works now. I think maybe it only needed the full file path, even though it was all in the same directory.

Run a python script from bamboo

I'm trying to run a python script from bamboo. I created a script task and wrote inline "python myFile.py". Should I be listing the full path for python?
I changed the working directory to the location of myFile.py so that is not a problem. Is there anything else I need to do within the configuration plan to properly run this script? It isn't running but I know it should be running because the script works fine from terminal on my local machine. Thanks
I run a lot of python tasks from bamboo, so it is possible. Using the Script task is generally painless...
You should be able to use your script task to run the commands directly and have stdout written to the logs. Since this is true, you can run:
'which python' -- Output the path of which python that is being ran.
'pip list' -- Output a list of which modules are installed with pip.
You should verify that the output from the above commands matches the output when ran from the server. I'm guessing they won't match up and once that is addressed, everything will work fine.
If not, comment back and we can look at a few other things.
For the future, there are a handful of different ways you can package things with python which could assist with this problem (e.g. automatically installing missing modules, etc).
You can also use the Script Task directly with an inline Python script to run your myFile.py:
/usr/bin/python <<EOF
print "Hello, World!"
EOF
Check this page for a more complex example:
https://www.langhornweb.com/display/BAT/Run+Python+script+as+a+Bamboo+task?desktop=true&macroName=seo-metadata

Python - running a script as a service with a name other than 'python'

I have a set of python scripts which I run as a daemon services. These all work great, but when all the scripts are running and I use top -u <USER>, I see all my scripts running as python.
I would really like to know which script is running under which process id. So is there any way to execute a python script as a different process name?
I'm stuck here, and I'm not ever sure what terms to Google. :-)
Note: I'm using Ubuntu Linux. Not sure if the OS matters or not.
Try using setproctitle. It should work fine on Linux.
Don't have a linux system here to test this on appropriately, but if the above doesn't work, you should be able to use the same trick they use for things like gzip etc.
The script has to tell what to run it at the top like this:
#!/usr/local/bin/python
Use a softlink like this:
ln -s /usr/local/bin/python ~/bin/myutil
Then just change your script to
#!~/bin/myutil
and it should show up that way instead. You may need to use a hard link instead of a soft link.
Launching a python script using the python script itself (and file associations and/or shell magic) is not very portable, but you can use similar methods on nearly any OS.
The easiest way to get this is using she bang. The first line of your python script should be:
#!/usr/bin/python
or
#!/usr/bin/python3
depending upon whether you use python or python3
and then assign executable permissions to the script as follows:
chmod +x <scriptname>
and then run the script as
./scriptname
this will show up as scriptname in top.

pass variable from python to shell script

HI guys i am new to scripting, i am doing small project in python and wxpython, which does automation. Now i have a problem while sending variable from python to shell script.
I have to connect ftp ,then download a file which is written in BASH(1st script). That bash script call one more bash script(2nd script) which is also in ftp server. BUT the 2 script name is not hard-coded in 1st script. so i have to send that "script name" from my python script which is i am not able to do right now.
so i need yours help to solve my issue, have gone through google but couldn't find anything.
Thanks in advance.
I have tried directly to run this but still it don't take 2nd argument,so i have gone through the script once. found that,
when $2 $3 $7 already configured inside the 1st script (those are in built in myproject.sh)
please help me to solve this issue..
The quickest and easiest way is to let your Python script create a file that sets a variable name using bash syntax, and then let your first bash script source it. Sourcing it will basically set that variable.
So in Python, you want to output that file somewhere:
name_of_script="/path/to/second/script.sh"
with open("/my/path/scriptnamevar.sh", "w") as f:
#Output to bash-recognized variable setting syntax
f.write("SCRIPT_NAME={}".format(name_of_script))
Then in your first script, you just need to source scriptnamevar.sh (or whatever you decide to call it).
source /my/path/scriptnamevar.sh
# This should output whatever name_of_script was in python
echo $SCRIPT_NAME
You could make use of command line arguments. Since you're using subprocess to call the first bash script, you could modify it to look like this:
subprocess.call(["bashScript1Name", "bashScript2Name"])
Then in bashScript1Name the name of the second script will be in the variable $1

Execute Python with pygtk without terminal appear

I have a little issue using pygtk, i have this project written in python, now the program works correctly, i have already added the string for the execution using terminal:
#! /usr/bin/env python
Now i want to know how to exec this script hiding the terminal that pop out once you double click the file.
Anyone knows?
Try saving it as a .pyc and executing that one.
Take a look at this and this.
Basically this:
import py_compile
py_compile.compile('abc.py')

Categories