Executing python module with Click command using command line - python

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

Related

Can't open file 'demo.py': [Errno 2] No such file or directory when running through a bash script

I have a folder named test-folder with two files.
# file name: demo.py
print('hello world')
# file name: script.sh
python3.7 demo.py
The test-folder is present inside /home/username/Documents/
I have exported the above path to the .bashrc.
Now when I try to execute the script.sh using the following command,
bash script.sh
I get the following error
python3.7: can't open file 'demo.py': [Errno 2] No such file or directory
How can I solve this? I'm on Ubuntu 18.04
Move to the folder containing the file :
$ cd /home/username/Documents/test-folder
after making sure you are in the folder ccontaining the file try:
$ python file_name.py
if it doesn't work try the full path to python and the full path to the file like this:
$ /usr/lib/python3.7 /home/username/Documents/test-folder/file_name.py
If you want to use script.sh from anywhere, your approach won't work.
When executing $ bash script.sh, the file script.sh is expected to be located in the working directory.
You can use absolute paths to solve this:
# filename: script.sh
python3.7 /home/username/Documents/test-folder/demo.py
Try executing this with bash /home/username/Documents/test-folder/script.py.
You can achieve your goal in an arguably simpler way by adding /home/username/Documents/test-folder to your PATH, adding a shebang to demo.py and making demo.sh executable.
$ export PATH=/home/username/Documents/test-folder:$PATH
$ chmod +x /home/username/Documents/test-folder/demo.py
demo.sh will look like this with a shebang:
#!/usr/lib/python3.7
print('hello world')
and can be executed from anywhere with $ demo.sh.
It works for me. Is there anything about your setup that is different than mine?
$ cat demo.py
print("Hello World")
$ cat script.sh
python3.7 demo.py
$ tree
.
└── test-folder
├── demo.py
└── script.sh
1 directory, 2 files
$ cd test-folder/
$ bash script.sh
Hello World
Edit: Try the following.
$ cat script.sh
python3.7 "`dirname "$0"`/demo.py"
If your script.sh file is only there to run the Python script, it’s not needed. Instead make the Python script itself executable.
(1) add this as the first line in your Python file — this tells Unix which interpreter to use when running this file:
#!/usr/bin/env python37
(2) make your Python file executable so it can be run directly:
chmod +x demo.py
(3) put that file on the path, e.g. a path you set via a Bash config file, or one that’s already on the path like /usr/local/bin.
Now any time you give the command demo.py your script will run. Note the .py extension isn’t required — you can name the file anything you like.

Python 2.7: Give input to python scripted EXE file

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

Using cron to launch python script in terminal window after reboot

I am trying to launch a python script automatically after boot. I want to launch it in a terminal window because the program gives important feedback in the terminal.
I've researched many ways to do this including crontab, init.d, rc.local, and /etc/xdg/autostart/myscript.desktop.
When I've tested my script manually, calling rc.local from the terminal works, however none of these work after booting.
I've tried many variations, the latest in crontab:
#reboot sleep 60 && xterm -hold -e sudo python /home/pi/newcode/newcode/boot-test.py
Other variations I tried include (calling my python script from a shell script):
#reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh
and
#reboot sleep 60 && /home/pi/bin/mount.sh && sh /home/pi/foo1.sh
Update:
foo1.desktop (saved at /usr/local/bin/foo1):
[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;
foo1 (saved at /etc/xdg/autostart/foo1.desktop):
#!/bin/bash
/usr/bin/x-terminal-emulator --command="/home/pi/newcode/newcode/boot-test.py" --title="My foo1"
python script, very simple for now (saved at /home/pi/newcode/newcode/boot-test.py)
sensortype=raw_input("press enter to continue")
Comment:
1. is named foo1.desktop ?
2. is named foo1.sh ?
3. does foo1.sh need to be made executable?
Yes, must be named *.desktop
If you use *.sh, the entry in *.desktop have to be equal
Exec=gksu /usr/local/bin/foo1.sh
Yes, a Bash script have to made executable.
Question: I've test my script (rc.local) manually
Don't use etc/rc.local, it's executed to early for X11.
My /etc/xdg/autostart example:
Create a foo1.desktop with at least the following content:
[Desktop Entry]
...
Name=foo1
Exec=gksu /usr/local/bin/foo1
Terminal=false
Type=Application
Categories=GTK;System;TerminalEmulator;
Copy foo1.desktop or create link to foo1.desktop in /etc/xdg/autostart.
Create /usr/local/bin/foo1 with the following content:
#!/bin/bash
/usr/bin/x-terminal-emulator --command="python path_to_your_*.py" --title="My foo1"
Make /usr/local/bin/foo1, executable.
After trying multiple variation the script that worked contained the following:
#!/bin/bash
/usr/bin/x-terminal-emulator -hold -e sudo python /home/pi/newcode/newcode/hms2-v2.6.py
making the script executable by do the following was also necessary:
sudo chmod 755 /etc/xdg/autostart/foo1.desktop
I never got cron or init.d to work

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.

where to save a file in cygwin

noobiest question ever:
I'm trying to work with python via cygwin on my pc laptop - I have a file (foo.py) but python can't find it. The error it's giving me is:
$ chmod +x foo.py
chmod: cannot access `foo.py': No such file or directory
Is there a special location within the Cygwin folder that I need to save my foo.py?
Thanks!
AP
It's not python that can't find your file, it's the chmod command. C drive is mapped to /cygdrive/c in Cygwin, and D drive is mapped to /cygdrive/d and so on and so forth.
Are you in the same directory as the file when you are running chmod?
If your file is at C:\mycode\python\foo.py then you should either change to that directory first -
$ cd c:
$ cd mycode/python/
or as #Ahmed mentioned above, you could also run the command as
$ chmod +x /cygdrive/c/mycode/python/foo.py
But you only need chmod if your python script starts with
#!/bin/python
To execute such a file, you'd say
$ /cygdrive/c/mycode/python/foo.py
Or if you are in the same directory
./foo.py
If the first line of the python script isn't "#!/bin/python" then you can skip the chmod and just type
python /cygdrive/c/mycode/python/foo.py
You go right click on foo.py figure out its full path then do this:
chmod +x foos-full-directory/foo.py
And this should work for you and btw its not Python problem it's your pwd other than the foo.py working directory and you even didn't use python word in your command.

Categories

Resources