configparser.NosectionError found when run a .exe generated from pycharm (Python) - python-3.x

I have created an exe file from pycharm using pyinstaller --onefile -w ./workflow/Workflow3.py
I have my config.ini located under folder resources.
i have used
config = configparser.ConfigParser()
# Read in config.ini from a path
config.read('../resources/config.ini')
ALternatively i have used all the possible combinations like absolute path. But i am getting below error when i directly run .exe.At the same time its working in pycharm

Related

How to include files while compiling?

I am using pyinstaller to compile a python script to an exe, but before I do that I want to obfuscate it using pyarmor, I fired up pyarmor web ui using pyarmor-webui and created a build that gives out a file named the same one as the file I want to compile but obfuscated and a folder containing two files __init__.py and _pytransform.dll now when I try to compile the obfuscated code into an exe using pyinstaller --onefile myfile.py I get an executable that when I run from a batchfile to look at the output it throws the following error Could not find "C:\Users\0000\AppData\Local\Temp\_MEI72482\pytransform\platforms\windows\x86_64\_pytransform.dll"
myfile.py
import os
import random
for i in range (10):
print (random.randrange(1,100))
Batch file
try_01.exe
pause
How and what do I need to add to pyinstaller command so that it includes the .dll file in the compilation process
The pyinstaller docs say the following:
--add-binary <SRC;DEST or SRC:DEST>
Additional binary files to be added to the executable. See the --add-data option for more details. This option can be used multiple times.
I haven't used pyarmor+pyinstaller before but it should be as simple as:
pyinstaller --onefile myfile.py --add-binary _pytransform.dll
Or, since I looked more into pyarmor's output, it may be:
pyinstaller --onefile myfile.py _pytransform.dll

how to Run .py file

I have written a "random movie suggester" code that will basically read the files of the directory where the .py file is stored and randomly pick one of them and print it out.
My problem is -
How to make .py file simple to run for non-technical person?
(e.g. I tried .bat but i had to hard code the path of the file).
Other solutions ask to install IDEs and run it.
First you have to install python on your computer.
Then you can type in your terminal:
python [the file name or the path to access it]
But I don't think this is what you are looking for...
You can create a .exe file with your python program using the pyinstaller module then running this in your terminal.
pip pyinstaller
pyinstaller --onefile [the name of the file]
The first line installs the module using pip.
The second line will create a .exe file that does exactly the same as your python code.

Adding textfile with --onefile option

the --add-data option works well without using --onefile option
I want the data text files to be compressed and merged into an exe file. But exe file only works when the data text file is in the same folder.
How can I merge the text file into the only one exe file??
When I used:
pyinstaller --add-data ="\GameUserSettings.ini;." file.py
it works.
pyinstaller --onefile --add-data ="\GameUserSettings.ini;." file.py
it can make the file.exe but it doesn't work well. and it works only when the text file is in the same folder.
When you add your text file with one-dir, Pyinstaller would put your file next to your executable and you will be fine when accessing your file by the script (e.g open("myfile.txt"). But when you create your executable with --onefile it would extract your file in a temp directory which is a separate directory, so calling open("myfile.txt") would result in a NotFound error because the file doesn't exist besides your executable. So you need to change the path to point to the temp directory. The sys._MEIPASS would return the temp directory so you need to locate your files inside it. You can find more info in here.
A function like this would solve the problem:
import sys
import os
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Then you can access your file with source = resource_path("myfile.txt").

How to change .py to .exe

I made a program in python which is in .py format. However, I would like it to be in .exe
The options I have found are:
py2exe
pyinstaller
The problems with those 2 are because I am running python 3.6 and those 2 programs do not support it, HELP!
Python 3.6 still isn't supported by Pyinstaller. So in order to use it you're gonna need Python 3.5 or below. So, you might wanna change your python version to 3.4 or 2.7. Then you can follow the procedure for creating the exe with pyinstaller. Also I think pyinstaller is a better option than py2exe.
However to make things work without switching to other versions, you might wanna try using cx_freeze:
1.Install the latest version of cx_freeze by pip install cx_Freeze.
2.Create a new python file named ‘setup.py’ on the current directory of your script.
3.On the setup.py, code this and save it:(here my prog.py refers to your .py file name)
from cx_Freeze import setup, Executable
base = None
executables = [Executable("my prog.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "<any name>",
options = options,
version = "<any number>",
description = '<any description>',
executables = executables
)
4.With shift pressed right click on the same directory to open a command prompt window.
5.Type: python setup.py build.
6.Check the newly created folder ‘build‘. Within that folder you can able to find your application.
pyinstaller
pip install pyinstaller
Basic usage is very simple, just run it against your main script:
pyinstaller /path/to/yourscript.py
cx_Freeze
pip install cx-Freeze
auto-py-to-exe
pip install auto-py-to-exe
Then to run it, execute the following in the terminal:
auto-py-to-exe
With a simple google search, I found this question which gives a link to a version of py2exe which happens to support python 3.3 and up. Here is the link!
On that page you will find the following bit of information:
Py2exe is a distutils extension which allows to build standalone Windows executable programs (32-bit and 64-bit) from Python scripts; Python 3.3 and later are supported. It can build console executables, windows (GUI) executables, windows services, and DLL/EXE COM servers.
Notice:
Python 3.3 and later are supported.
I have created a batch file to do the work, but first, go to https://datatofish.com/add-python-to-windows-path/ and add python to your path.
Then create a new notepad file with this:
SET /P _input1= Enter the file directory:
cd %_input1%
SET /P _input2= Enter the file name:
pyinstaller --onefile %_input2%
ECHO The exe file is in the dict folder in %_input1%
pause
Save this as a .bat file anywhere and run it.
Enter the directory of the python script.
then the name.
Hope this helps!

How to create .EXE file in python using cx_freeze

I have one application developed in python 3.2, which has inbuilt modules(ex: Tkinter, matplotlib, openpyxl), user defined modules & classes(ex: draw_graph, generate_report), icon files, log file, .csv, .docx etc. I am running this application from script(ex: testapplication.py)
I have setup file as
import sys
from cx_Freeze import setup, Executable
exe = Executable(
script=r"C:\Python32\testapplication.py",
base="Win32GUI",
)
setup(
name = "TESTApp",
version = "0.1",
description = "An example",
executables = [exe]
)
Now I want to create a exe file of this application. can anyone please suggest me a way to do this?
So this is what you need to do. For starters, change script=r"C:\Python32\testapplication.py" to script=r"testapplication.py"
Then, put ALL the files to need to convert into C/python32 including the setup file. Then what you wan to do is get your command line up, and type the following commands: (assuming that you're cx_freeze file is named setup.py):
cd
cd python32
python setup.py build
And then you should have a build folder in that directory containing the exe file.

Resources