python program compiled to cython
- not open the terminal
- point to my virtualenv
>>> import my_program
Instead would like to make it more user friendly and start the program from an icon
python program compiled to cython
>>> import my_program
Instead would like to make it more user friendly and start the program from an icon
I'm not sure what operating system you are on. However, the answer is simular for both.
On Windows:
You need to create a .bat file. The are already questions about how to do this to use Python. The .bat file is just a text file with a list of commands you want to execute (i.e. whatever you normally type into the command prompt). The Python line would be:
C:\path\to\python\pythonw.exe -m my_program %*
pythonw
instead of Python avoids creating a window. %*
passes any command line arguments to your program (not necessary, but probably good practice). The -m
version your program as the __main__
module.
edit: -m
doesn't work with compiled (Cython) modules. To run these you should do C:\path\to\python\pythonw.exe -c "import my_program" %*
instead.
The .bat file should be executable by clicking on it.
On Linux (and OSX too, I think):
You want a shell script, instead of a .bat script. Again, it's just a list of commands to execute (so what you normally type in to the terminal). Your python line should be
python -m my_program $@
or (if my_program
is a compiled module)
python -c "import my_program" $@
The $@
is forwarding command line arguments. You then need to make the shell script executable. See this very comprehensive question. Once you've made it executable you should be able to click on it to run it.
ON Linux (Ubuntu) - create icon for Cython compilation .so file (Python) and open virtualenv if you have one setup
create a shell script open text editor (file) and type the following lines
#below is a line you need to put at the top
#!/bin/bash
#activate virtual environment
source virtual_environment_name/bin/activate
# change to the directory where the Cython .so file is
cd /home/user
#import the .so file (name only)
python -c "import my_program" $@
safe file name.sh in your home directory (the directory the terminal start in) or put the correct path in your .desktop file
create an desktop icon with the following open text editor and type
[Desktop Entry]
Type=Application
Terminal=true
Name=Name_of_the_icon
#the icon to use for the launcher
Icon=/home/user/picture.PNG
#executes the shell script we created
Exec=gnome-terminal -e "bash -c './name_of_shell_script.sh;$SHELL'"
Categories=Application;
now safe the file with the extension .desktop
ImIMPORtant:
you have to make both these files executable: right click, properties, permissions tick the the box make executable or run chmod +x filename in terminal
now click icon on desktop to run your Cython compilation (python program) (you can also just click on the .sh file to run the program if you don't want to create a specific icon)
if you remove ;$SHELL
from the gnome-terminal -e "bash -c './name_of_shell_script.sh;$SHELL'"
then the terminal also closes after the program is closed
IF you only have a normal python program (not compiled), you only
create a .desktop file and put #!/usr/bin/env python
at the beginning of
your script(python program), and put the path of your python program instead of the .sh file inside the .desktop file. AND MAKE .PY FILE EXECUTABLE