Configuring Pycharm to run Pyinstaller - python

Yes I want to create a run configuration in PyCharm to run Pyinstaller and get my executable. According to the Pyinstaller documentation you should be able to locate an python script called pyinstaller-folder/pyinstaller.py after the installation, but it wasn't there. Then I look carefully and found this other one named pyinstaller-folder/__main__.py which should be the same <--(me wild guessing), so I set up my running configuration like this:
After running it, is giving me this error:
/usr/local/Cellar/python3/3.4.3/bin/python3.4 /usr/local/lib/python3.4/sit
e-packages/PyInstaller/__main__.py --onefile --nowindow --osx-bundle-identifier=jg.optimizer -F --name=genoptimizer optimizer/manage.py
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/PyInstaller/__main__.py", line 26, in <module>
from . import __version__
SystemError: Parent module '' not loaded, cannot perform relative import
Process finished with exit code 1
It seems to require a parent module to run but, how would that look like?

After more than two years, perhaps there is a better option.
In the PyCharm menu go to File -> Settings.
In the Settings dialog find Tools -> External tools and use the green + to add a new external tool.
For example:
Then, the IDE will allow you to run it on any python script. Right click on the file and the context menu will show External tools -> PyInstaller.

The PyInstaller package is a runnable module and can be run using python -m PyInstaller. To configure it as a run target in PyCharm, leave the "Script" field blank, write -m PyInstaller in the "Interpreter Options" field, and put the PyInstaller parameters into the "Script Parameters" field.
For example:
PyCharm will complain that the "Script" field is empty, but it will let you run the configuration anyway.

The run command has changed a bit since the accepted answer. You can now select a module to launch rather than editing the interpreter options.
As of writing this answer, here is how it looks like:
Notes:
This solution requires to install PyInstaller in the virtual environment of the project.
I am using PyCharm pro 2020.1
Old solution should still work
Using external tool is still a possibility. Which solution you choose is a matter of personal preference.

Install pyinstaller in pycharm, then open the pycharm terminal and write python -m PyInstaller.

So as Pycharm has newly updates, my case was a bit different as I installed pyinstaller from the interpreter settings as the following picture shows:
For Linux Users:
You could install it in both Python 2.7 or Python 3.7+. Make sure to get the path of where pyinstaller was stored.Then in the Settings option, try to find Tools -> External tools and add a new external tool as the following picture shows:
For Windows users:
If you are using Pycharm or any virtual environment. Unfortunatelly Pycharm creates its local vertual environment in venvpath once you indicate the interpreter. So, you should set the external tool (pyinstaller) to the real path of your python 3.7 .exe as the picture shows

For those of us on Windows with Anaconda trying to figure this out, I found it easiest to just set up a Bash Configuration (I believe you need the BashSupport plugin for this), and set:
Script: pyinstaller (assuming pyinstaller is in your path, if not, the full path)
Interpreter path: C:\Windows\system32\cmd.exe (yes, a bash configuration can just use the standard command program)
Interpreter options: /C
Program arguments: script_name.py (along with any other pyinstaller arguments)
Working Directory: C:\Path\To\Script

Related

Python 3 install error. This file does not have a program associated

I have encountered an error while try to run a python 3.6.4 script after upgrading from 2.7.
I had initial installed both python 2.7 and 3.6 but them deleted 2.7 because I had no use for it and i am not getting this error.
I have tried:
Reinstalling python 3.6.4
changing the install location of said install
Repairing the python 3.6 install
Error:
A Python script by itself is not an executable program. So if you want to run the script Evosim.py by just entering Evosim.py in the command line, the .py file ending needs to be associated with the Python interpreter. It seems that currently in your system this is not the case.
There are two ways to deal with your situation:
Start your script by entering python Evosim.py instead of only Evosim.py in the command line.
Fix the broken association of the .py file ending with the Python interpreter. The answers to this question migth help you with that.
Solution #1
Go to your registry by searching regedit.
Open category HKEY_CLASSES_ROOT. Select .py. If .py don't exists then make it by yourself.
Double click the Default. It will make a popup. Change its value to Python.IDLE and click OK. Make a new String key in .py naming Content Type with value text/plain.
Solution #2
If the above method don't works then just go ahead and reinstall your python (also uninstall Python Launcher). Your modules and files will be at their place.
You can now open .py files.
Running command prompt as an administrator resolved my issue, and able to execute my python script
1). Right click any python file
2). Click Open with then Choose another app
3). Click on More apps then Look for another app on this PC. It should open up file explorer
4). Select python.exe wherever it is located.
A sample path is C:\Users\<user>\AppData\Local\Programs\Python\Python38\python.exe for a user installation. A way to check this path is by typing where python into cmd; it will list all installations of Python.
Now the python.exe you chose is associated with all .py files and typing file.py should not give an error.

How can I run my Python program like a normal program from the desktop?

I'm creating a program that I would like to use as a normal program as well as continue to code it on the side. To do this I first tried creating a shortcut of the .py file in my PyCharms project folder and sent it to desktop. When I double-clicked the shortcut the command prompt would open for a second and then shut. It's a PyQt4 program so I'm not sure if this has any bearing. The program has been coded in Python 3.4. I've noticed that when I open the command prompt and type 'python' it shows Python 3.5 for some reason so I'm not sure if this has any bearing on the situation.
If you've ever programmed in C# I'd like to be able to build a solution and then rebuild the solution when I've updated the code so that I can access the program as a normal program as well as continue to improve the code of it.
Thanks for any help.
Managed it thanks to the link above. Uninstalled Python 3.5 and set my PATH variable to C:\Python34. Downloaded pyinstaller and installed it using PIP. Then navigated to Python34/Scripts and dragged myFile.py (the one to be made an .exe) into it. Ran pyinstaller.exe --windowed myFile.py to create the exe which then went to my dist folder. Created a shortcut and it worked perfectly.
Go to your environmental variables (Right click on Computer > Properties > Advanced system settings > Environment Variables...). Find Path in System variables, select it, and click edit. Remove the Python 3.5 path and replace it with your python 3.4 or virtual environment folder that has python.exe in it.
Make a shortcut on your desktop that points to the .py file that you are editing.
If you have all of the dependencies right then double clicking the .py file's shortcut should run your program.
Other wise you can pip install cx_freeze and use cx_freeze like setuptools. Create a setup.py file and build the executable.
If you want to install this executable I suggest using Inno Setup. It is pretty straight forward on how to use and has an easy wizard that helps you build a basic installer.

Windows: run python command from clickable icon

I have a python script I run using Cygwin and I'd like to create a clickable icon on the windows desktop that could run this script without opening Cygwin and entering in the commands by hand. how can I do this?
The simplest solution will be creating batch file containing command like:
c:\python27\python.exe c:\somescript.py
With this solution you will have to have installed python interpreter. If you need more portable solution you can try for e.g. py2exe and bundle python scripts into executable file, able to run without requiring a Python installation.
This comes straight from Python Docs (https://docs.python.org/3.3/using/windows.html):
3.3.5. Executing scripts without the Python launcher
Without the Python launcher installed, Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.
You can also make all .py scripts execute with pythonw.exe, setting this through the usual facilities, for example (might require administrative rights):
Launch a command prompt.
Associate the correct file group with .py scripts:
assoc .py=Python.File
Redirect all Python files to the new executable:
ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
This tutorial shows how to create a batch file that runs a python script.
Note that many of the other answers are out of date - py2exe is out of support past python 3.4. More info here.
As an alternative to py2exe you could use the pyinstaller package with the onefile flag. This is a solution which works for python 3.x.
install pyinstaller via pip
Package your file to a single exe with the onefile flag
pyinstaller --onefile your_file.py
A better way (in my opinion):
Create a shortcut:
Set the target to
%systemroot%\System32\cmd.exe /c "python C:\Users\MyUsername\Documents\MyScript.py"
Start In:
C:\Users\MyUsername\Documents\
Obviously change the path to the location of your script. May need to add escaped quotes if there is a space in it.
The solution that worked like a charm for me >
From https://www.tutorialexample.com/convert-python-script-to-exe-using-auto-py-to-exe-library-python-tutorial/
pip install auto-py-to-exe
The GUI is available just by typing:
auto-py-to-exe
Then, I used this command to generate the desired output:
pyinstaller --noconfirm --onedir --windowed --icon "path/favicon.ico" "path/your_python_script"
Now I have my script as executable on taskbar

Is it possible to easily extract python run configuration (with additional path) from Pycharm?

I have a working Python project on my PC, which I am running from Pycharm.
It uses Pyroot (an interface to Root C++ library), whose C++ lib path I have added in Project Settings/Python Interpreter/Paths in Pycharm. It also needs to use the 2.7 Python interpreter, instead of 3., which is a default python in my terminal.
I want to run this project remotely on another desktop, so I need to be able to run it from terminal specifying the path to Root and the interpreter version.
Is there a way to easily extract from Pycharm the exact run command it is using when I'm running the code via run button?
Alternatively, if that's impossible, how should I specify the path to Root and the interpreter version when running from terminal?
I guess to best way is to create a virtualenv either in the terminal or in pycharm including the corrext python version 2.7 and install pyroot via pip into this virtualenv. Then you can simply ssh in the remote host, activate the venv and start your project from the terminal. Or you ssh into it with X-forwarding and start Pycharm itself from your client.
If you select the correct project and go to File > Settings, under the Project Settings you can see the Project Interpreter which tells you which interpreter is being used.
Hope this is what you are looking for.

How to associate py extension with python launcher on Mac OS X?

Does anyone know how to associate the py extension with the python interpreter on Mac OS X 10.5.7? I have gotten as far as selecting the application with which to associate it (/System/Library/Frameworks/Python.framework/Versions/2.5/bin/python), but the python executable appears as a non-selectable grayed-out item. Any ideas?
The python.org OS X Python installers include an application called "Python Launcher.app" which does exactly what you want. It gets installed into /Applications /Python n.n/ for n.n > 2.6 or /Applications/MacPython n.n/ for 2.5 and earlier. In its preference panel, you can specify which Python executable to launch; it can be any command-line path, including the Apple-installed one at /usr/bin/python2.5. You will also need to ensure that .py is associated with "Python Launcher"; you can use the Finder's Get Info command to do that as described elsewhere. Be aware, though, that this could be a security risk if downloaded .py scripts are automatically launched by your browser(s). (Note, the Apple-supplied Python in 10.5 does not include "Python Launcher.app").
The file associations are done with the "Get Info". You select your .PY file, select the File menu; Get Info menu item.
Mid-way down the Get Info page is "Open With".
You can pick the Python Launcher. There's a Change All.. button that changes the association for all .py files.
Steve, add the following to the top of your python script:
#!/usr/bin/env python
It must occur as the first line of the file.
Then make the file executable as so:
daves-macbookpro ~: chmod +x foo.py
Then all you need to do to run this is type
./foo.py
The default python installation (atleast on 10.6.8) includes the Python Launcher.app in /System/Library/Frameworks/Python.framework/Resources/, which is aliased to the latest/current version of Python installed on the system. This application launches terminal and sets the right environment to run the script.

Categories

Resources