-2

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

DavidW
  • 29,336
  • 6
  • 55
  • 86
Cat
  • 324
  • 2
  • 8
  • Welcome to SO. Please read this [how-to-ask](http://stackoverflow.com/help/how-to-ask) to improve your question with sufficient and specific information to describe your problem. – thewaywewere Apr 23 '17 at 11:33
  • i tried explaining the problem in detail but had to change it somehow just to get it posted and in an acceptable format – Cat Apr 23 '17 at 12:18
  • i wrote a program in python, then compiled it to cython, it is working fine, – Cat Apr 23 '17 at 12:19
  • but i want to my client to just click a icon to start the program instead of starting the program in the terminal – Cat Apr 23 '17 at 12:19
  • Possible duplicate of [How To Add An Icon Of My Own To A Python Program](https://stackoverflow.com/questions/16782047/how-to-add-an-icon-of-my-own-to-a-python-program) – Jack Moody Mar 07 '19 at 14:27

2 Answers2

2

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.

DavidW
  • 29,336
  • 6
  • 55
  • 86
  • thank you so much, i will upvote as soon as i try all this, thx – Cat Apr 24 '17 at 06:35
  • Linux (client is on OSX) – Cat Apr 24 '17 at 06:49
  • And you do realize you'll have to recompile the Cython module on OSX for the client? (A module compiled on Linux won't work on OSX) – DavidW Apr 24 '17 at 06:54
  • hi, sorry life happened, but back now, want to try what you explained, i'm on Linux. not sure what exactly i should do. i ussaully type – Cat Apr 23 '18 at 10:56
  • so will i create a textfile with python -m my_program $@ import my_program? i first go to virtuelenv in terminal, does this matter? – Cat Apr 23 '18 at 10:59
  • chmod +x sh script.sh python -m version1 $@ import version1 – Cat Apr 23 '18 at 11:03
  • ?? (is this all my test file need) do i change te ext. to exe? – Cat Apr 23 '18 at 11:04
  • i mean textfile – Cat Apr 23 '18 at 11:05
  • I realised that `-m` doesn't work with compiled modules so I've edited the answer slightly. If you normally set up a virtualenv then you should do that in the script. You need to add the `#!/bin/bash` line given in the link too. You don't need to change the extension to exe (that only matters on windows) – DavidW Apr 27 '18 at 17:38
  • ok thx, only got around it now, it works (without #!bin/bash line) have to figure out how to activate virtualenv, i read what you suggest, does not exactly know how to activate the virtual environment within this script, suppose not so difficult, but does not know exact syntax. will try and get back with feedback when succeed – Cat Mar 03 '19 at 16:44
  • hi David, do i start the virtualenv (or miniconda) in the shell script or my actual program script? if you know what i mean?i tried so many things and if you dont know exactly its as good as knowing nothing... my program is running fine as .py executable with Linux created icon, but how to run the Cython compiled version is a nightmare, at least for me, for now... – Cat Mar 04 '19 at 19:11
  • i can run Cython compilation in terminal, but need an icon on ubuntu for users – Cat Mar 04 '19 at 19:13
  • You start the virtualenv in the shell script. The shell script should just be the list of commands you type in the terminal to run your program. – DavidW Mar 04 '19 at 19:47
  • ok thx so much i will ty again, do i need the $@ after each command – Cat Mar 04 '19 at 19:55
  • No - the `$@` forwards command line arguments that were passed to the script (i.e. so if you did `./myscript filename.txt` it would call python with `python -m my_program filename.txt`). Only include it if it's useful to pass command line arguments to your Python script (e.g. a file to load) and only put it on the Python line – DavidW Mar 04 '19 at 20:02
  • appreciate your help – Cat Mar 04 '19 at 21:06
  • #!/bin/bash cd Desktop conda activate conda_environment python3 -c 'import stock' $@ – Cat Mar 05 '19 at 09:26
  • above is in the shell script, but error is Your shell has not been properly configured to use 'conda activate'. im investigating, seems is need to remove a line in the bashrc file, but scared to do anything there, the line they want me to remove is in an else statement, so surely i need to replace it with pass or remove the else alltogether, not sure – Cat Mar 05 '19 at 09:29
  • ok i've figured it out, is quite simple, when you know, will post add my answer – Cat Mar 06 '19 at 07:38
0

ON Linux (Ubuntu) - create icon for Cython compilation .so file (Python) and open virtualenv if you have one setup

  1. 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

  1. 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

Cat
  • 324
  • 2
  • 8