Executing a python script when a terminal is opened within that terminal - python

Im interested if there is a way to run a python script when you open a terminal window. For example
print "hello world"
Every time i open a terminal, hello world would appear.

If you are using bash, anything you put in your ~/.bashrc file will be run when you open the terminal, i.e.
python my_script.py
will execute the script my_script.py.

Every time i open a terminal, hello world would appear.
Just do:
clrscr("Hello World") # or whatever string you want
anywhere in any of your python scripts.
To achieve this effect, you have to do the following 2 things
1- For sake of portability you have to make a small module as shown below-
# goodManners.py
from os import system as command # for calling to system's terminal
from platform import system as osName # for getting the OS's name
def clrscr(text):
if osName()=='Windows':
command('cls')
else:
command('clear')
print(text)
2- Now in your ~/.bashrc:
export PYTHONSTARTUP=$HOME/.pythonstartup
and put your python code in $HOME/.pythonstartup, like:
from goodManners import clrscr

Related

Exporting variables from Shell script and accessing it in Python script

I am trying to export a variable from my shell script and then access it from my python script.
#!/bin/bash
test="hello, python!!!"
export test
python test.py
My Python script is as follows:
#!/usr/bin/python
import os
text = os.environ.get("test")
print(text)
After I source my shell script I dont see the printed output on command line. Instead, I see:
shell_script:export:3: not valid in this context: test^M
But if I individually echo or run the python script after sourcing the shell script, I am able to get the desired result. Any ideas on how to do it in one go??

python opens new console window instead of printing into cmd

My Python 3 (3.6) opens new black console window for each Python command I run (pip is just the same).
Example: When I create a hello_world.py with a while True: print("hello world") Loop in it and start it from PowerShell via python test.py it will open a new window, print "hello world" in the new window and leave the console I used to run the script empty... (same happens with Cygwin) using CMD it won't even open the Python window and just closes gives me back a new CMD prompt-line.
Even worse when using PyCharm each time a background-task runs (e.g. syntax-check) a black console window opens for ~1 sec. and thus blocks me from writing code.
Example images to show what is happening:
Code used for test.py:
while True:
print("Hello World")
So far I tried:
creating and modifying manifest files for python.exe/pythonw.exe
reinstalling Python3
installing a different Python3 Version
changing the default codepage for CMD/PowerShell
setting PYTHONENCODING and PYTHONIOENCODING to UTF-8
starting PowerShell/Python as admin
//edit:
the Question is: How can I get back the default Python behavior, as seen on every other (Windows) Computer.
I found the solution is just run cmd or powershell as Administrator any other type of user would cause this problem.

How to execute Python scripts with python command

I want to run the Python Script by using python command from IDLE Python GUI.
The location of the file is C:\Users\DELL\Desktop\Hello.py
When I run python C:\Users\DELL\Desktop\Hello.py with Windows command promt, it works. However, when I try with IDLE Python GUI and Python (command line), it does not work and gives me a message SyntaxError: invalid syntax
Capture
inside python shell, you can import the module. This way, you can see "Hello" printed on shell once u import it.
>>> import sys
>>> sys.path.append('C:\Users\DELL\Desktop')
>>> import Hello
"Hello"
When using IDLE, you are already "inside" python (in its REPL)..
You should "load" (import) the file instead..
See here https://stackoverflow.com/a/21650698/5121955 (exact same situation), where you can find many solutions with their advantages..
You cannot do this from the Python interpreter, since this is not Python syntax, if you want to do this it has to be from command prompt or execute it as a system command:
import os
os.system('python C:\\Users\\DELL\Desktop\\Hello.py')
Or use subprocess.call
If you want to run your python file from another Python file see How can I make one python file run another?
If you want to run it from the IDLE simply select open and navigate to the desired location and select run.
If it is executable script (as you stated, it works from command line with the python command), you should be able to execute it directly from python with the following statements
import os
os.system("C:\\Users\\DELL\\Desktop\\Hello.py")

How does one get the output of fabric execution (fab.exe) on windows 7?

I am am interested in using Fabric as a deployment tool and I am having trouble making it through the tutorial. I believe I have it installed correctly (I used easy-install)
I have defined the sample function in fabfile.py:
def hello():
print("Hello world!")
I then open a command window and run
C:\dev>fab hello
A second cmd shell window opens and then closes again nearly instantaneously and I have no idea what it says.
I have tried opening the command window with the /K option
c:\dev>cmd /K "fab hello"
And I have tried redirecting the output to a file, but the file comes back empty.
c:\dev>fab hello >> output.txt
Any hints, suggestions or comments appreciated.
DiggyF's suggestion worked great for taming the cmd shell
fab hello > output.txt 2>&1
I now get the output of the instantaneous shell execution piped to a file.
The output: ImportError: No module named win32api. This led me on a much longer goose chase that needs to be placed in another question.
The solution to getting fabric running on Windows 7:
easy_install fabric
manually install pycrypto-2.0.1.win-amd64-py2.6.exe from:
http://www.voidspace.org.uk/python/modules.shtml
manually install pywin32-214.win-amd64-py2.6.exe from:
http://sourceforge.net/projects/pywin32/files/
To keep the shell from closing right away after running, you can use:
from fabric.contrib.console import confirm
def hello():
...
confirm("Would you like to exit?")

Auto executable python file without opening from terminal?

Sorry if this is on the wrong site ( maybe superuser ) but I'm trying to make my python.py file executable so I can click on it and it automatically does its thing, without me specifying it to open in the terminal by that default prompt, and I already have 'chmod +x' for its permissions.
Clarification:
I want to run it by clicking on it, not through the terminal ( I meant that when I said 'can click on it and it automatically does its thing ' )
Already have a shebang line
When I click it right now, it prompts me with do you want to open it in a text file, terminal - can I make it always default to opening in the terminal or is this just an oddball request?
On the first line in your python file, add this:
#!/usr/bin/env python
So if you have:
print "Hello World"
You should then have:
#!/usr/bin/env python
print "Hello World"
First, pick a file extension you want for files you want to have this behavior. pyw is probably a good choice.
Name your file that, and in your file browser associate that file type with python. In GNOME, you'd open its Properties window, go to the Open With tab, and enter python as a custom command.
Now here's the important part: That little dialog you've been getting asking you what you'd like to do with the file is because it is marked as executable. Remove the executable bit with chmod -x. Now when you double click it, it will simply be opened with the associated program.
Of course, if you want to run it from the command line, you'll now have to start it with python explicitly since it isn't marked executable. The shebang line doesn't matter anymore, but I'd leave it in anyway in case someone else marks it executable and expects it to work.
http://supervisord.org is better choice.
Have you placed this at the beginning of the file:
#!/usr/bin/python
?
As others have said, you need put the "shebang" at the start of the file, to say which interpreter to use to execute the file.
As mentioned in the above link, the most portable way is to use the env command (instead of a fixed path to python) - put this as the first line in the file:
#!/usr/bin/env python
The shell will look in $PATH for your python, rather than looking for /usr/local/bin/python then failing. This means it will work if Python is installed in a non-standard location.
For example:
$ cat example.py
print "Test"
$ file example.py # it is treated as an ASCII file
example.py: ASCII text
$ chmod +x example.py
$ ./example.py # when executed, it defaults to being executed as a shell script
./example.py: line 1: print: command not found
Now, if I add the "shebang" line...
$ cat example.py
#!/usr/bin/env python
print "Test"
$ file example.py # it is recognised as a Python script
example.py: a python script text executable
$ ./example.py # and executes correctly
Test
I have anaconda installed and
#!/usr/bin/env python
did not work for me, however:
#!/home/geoff/miniconda3/bin/python
did work. So, check which python your terminal normally uses to execute your .py files with
which python
in a terminal and use that as your shebang.

Categories