Python 2.7: Give input to python scripted EXE file - python

I have written a simple program in python which takes input value and display result accordingly.
Program: Test.py
a=raw_input('Enter value1:')
b=raw_input('Enter value2:')
#Do some work here based on passed inputs
I have to run the program in the different system where there will not be python installed. So I created Test.exe file using pyinstaller.
Now when I run .exe it's getting closed within seconds.
Question: How can I give a message Enter value1: to user to put some input values by running .exe file?

make.bat
rem CREATE our test.py
echo a=raw_input('Enter value1:') > test.py
echo b=raw_input('Enter value2:') >> test.py
rem Install Pyinstaller
c:\python27\python -m pip install pyinstaller -U
rem Build IT!!!!!
c:\python27\scripts\pyinstaller --onefile test.py
rem RUN IT!!!!
dist\test.exe

Related

How to run bash commands from Python preferably with the os library?

I am trying to run the following python script named test.py. It contains multiple bash commands which I would like to execute in a Linux terminal (unix). This is the content of the file:
import os
os.system('echo install virtualenv')
os.system('sudo pip install virtualenv')
os.system('echo create virtual environment')
os.system('virtualenv my_virtualenvironment')
os.system('echo activate virtual environment')
os.system('source my_virtualenvironment/bin/activate')
I am running the Python script using the following in the terminal:
python3 test.py
The problem that I have is that the commands do not run the same way as they would on a Linux terminal. The output is the following error when trying to execute the last line of the Python script:
sh: 1: source: not found
The last command source my_virtualenvironment/bin/activate normally runs fine if I execute it directly in the terminal (without my Python script). Now, what does sh: 1: mean and why does it not work with my code? I would expect to get something starting with bash: .
Also I have found this solution, but I would like not to use lists for executing commands and maybe even to stick with the os library (if there is a simpler solution without os, I am also open for that):
https://stackoverflow.com/a/62355400/11535508
source is a bash built-in command, not an executable.
Use the full path to the python interpreter in your commands instead of venv activation, e.g. os.system('<venv>/bin/python ...').
The second option is to write your commands into a separate bash script and call it from python:
os.system('bash script.sh')

Executing python module with Click command using command line

I have created a directory 'prince' which consists of a subdirectory 'src' containing 'main.py' file in it. This main.py uses Click command for input from user using command line. So currently I have to set current working directory to 'prince' in terminal and then execute my module using the command
'$ python -m src.main run --s 10 20 30'
where 'run', '--s' are my click command inputs.
Is there an easy way to wrap 'python -m src.main' to something short to execute my module along with inputs using click command ? Or any better way to execute my module along with inputs from user ?
I hope this will answer your question.
In your src/main.py file you could add a #!/usr/bin/env python3 shebang which means that your program is run by python by default.
And you should give it execute permissions via
chmod +x src/main.py
That means you can now run
src/main run --s 10 20 30
Instead of
$ python -m src.main run --s 10 20 30
Antother way to achieve this would be:
Create a file in the "prince" directory named "start" with Content:
python3 -m src.main "$#"
Give it executable permissions:
chmod +x start
Now you can run:
./start run --s 10 20 30

How to run exe file with command line arguments in Mac terminal?

I want to run the exe file with command line arguments in Mac terminal
p1.exe -f input.txt
But im getting error -bash: p1: command not found
I have converted python file p1.py into p1.exe using
pyintsaller p1.py --onefile
And running the python file with arguments works
python p1.py -f input.txt
This isn't to do with Python, but is a basic command shell issue. To run an executable from the current directory, you need to use the ./ prefix.
./p1.exe -f input.txt
Note, it's a bit odd to use a .exe extension for a Linux executable.
Note that on Unix like systems (Linux/Unix/Solaris/MacOS). scripts can be run without explicitly invoking interpreter, if two conditions are meet:
script file starts with this line (or similar): #!/usr/bin/env python
file has executable attribute flag is set
Then you can run script like this:
./p1.py --onefile
./ means run thing from local directory. If this is not pressent the it tries to run things located by PATH variable, that is why you can run interpreter python

R equivalent to `python -i`

Typing python -i file.py at the command line runs file.py and then drops into the python terminal preserving the run environment.
https://docs.python.org/3/using/cmdline.html
Is there an equivalent in R?
I may be misinterpreting what python -i file.py does, but try:
From inside R, at the terminal, you can do:
source('file.R')
and it will run file.R, with the global environment reflecting what was done in file.R
If you're trying to run from the command line, review this post

How to modify the command to call Python

On my Mac, python will run Python 2 and python3 will run Python 3 in the terminal. How can I modify the command so I can call Python 3 with just py?
Add a new file in your home directory called .profile. In there, add a line:
alias py=python3
You'll have to start a new terminal session for this to have any effect.
Place you in the directory where the python3 executable is (with cd command).
Then, enter :
ln -s python3 py
This creates a symbolic link.

Categories

Resources