I just finished creating a python program in 2.7 and I converted it to a .exe with py2exe.
Everything works fine when I run the converted executable file in the folder I placed it in with all of the images in it. After converting the python program to .exe, I proceeded to creating a setup file for it. I added all of the files associated with my project including tkinter in the setup file. I added pretty much everything that let me run the executable.
Once I finished creating the setup file, I opened it. I went through everything and finished installing it on my system and created a shortcut on my Desktop. When I tried to open it, it would not work. Instead of running the program, it tells me to open a log file in its folder in the Program Files. When I open the log file, I noticed an error. How do I fix this?
Error:
Traceback (most recent call last):
File "gui.py", line 10, in <module>
File "Tkinter.pyc", line 1764, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
{C:/Program Files (x86)/lib/tcl8.5} {C:/Program Files (x86)/lib/tcl8.5} C:/lib/tcl8.5 {C:/Program Files (x86)/library} C:/library C:/tcl8.5.15/library C:/tcl8.5.15/library
This probably means that Tcl wasn't installed properly.
I found a bug on the virutalenv site which suggested the following https://github.com/pypa/virtualenv/issues/93
I imagine that you are encountering the same issue just without virtualenv
the following set the correct paths which can then be included in the application please find the right path to TCL and TK for your python version
set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"
set "TK_LIBRARY=C:\Python27\tcl\tk8.5"
restart your cmd or shell
I believe that the TCL location have changed from there default ones.
Related
I seem to be having a vscode related issue. I am doing the open() function but no matter what I ask it to do it gives me a directory error. The file that I want the python script to interact with is in the same folder so it should work but when I do "import os" and "os.getcwd()" the directory it says I am in is Desktop. (the script and file are both in the "/Desktop/Python/File Handling" directory)
It seems the script is stuck at the Desktop directory when I try to run it from vscode. If I run it by doing python3 "name of script" command in the kali linux terminal it works fine and if I check my directory again with os.getcwd() it says the correct one (/Desktop/Python/File Handling).
So I believe it's something with vscode as it literally just randomly happened one day. Yesterday my scripts were working fine and now all the ones I run from vscode, that are supposed to interact with the files in their respective folders, don't work. The vscode terminal gives me this code and as you can see it does the cd command at the start, which I believe might be the issue for why it always looks at files in the Desktop directory but I do not know how to make it stop doing that.
$ cd /home/kali/Desktop ; /usr/bin/env /bin/python /home/kali/.vscode/extensions/ms-python.python-2022.18.2/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 41017 -- /home/kali/Desktop/Python/File\ Handling/File\ Handling\ 2.py
Traceback (most recent call last):
File "/home/kali/Desktop/Python/File Handling/File Handling 2.py", line 3, in <module>
f = open("apple.jpeg", "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'apple.jpeg'
Lastly, I know about the os.chdir(r"/home/kali/Desktop/Python/File Handling") function and it fixes the issue but I do not want to have to write this command at the top of every script that is supposed to interact with the files in the folder it is in by default and because this issue just randomly came up out of nowhere the next day I opened vscode and ran my script from yesterday (without changing any code or vscode settings.)
P.S. I am using a VM as well if that helps.
This is caused by vscode using workspace as root floder.
This will lead to a problem. When you use the os.getcwd() method in the deep directory of the workspace, you will still get the workspace directory.
You can open your settings and search Python > Terminal: Execute In File Dir then check it.
You can also use debug mode and add the following to your launch.json:
"cwd": "${fileDirname}"
I want to transform a Python script to an .exe to share it with friends. I've tried with auto-py-to-exe, but here is what I get when I open it:
Traceback (most recent call last):
File "main.py", line 1, in <module>
ModuleNotFoundError: No module named 'pygame'
I saw somewhere that I had to indicate in auto-py-to-exe's interface the module name here:
But I still get the same error.
I have experienced this before where I export .py into .exe with --onedir setup. Here are the solutions that I might suggest (and works for me)
1. Export as --onefile
auto-py-to-exe screenshot
Exporting using --onefile creates only a single .exe file instead of having a folder contain hundreds of other supporting files (is that term exist? IDK). but the drawback is the file will be painstakingly slow to load up.
but if you export it as --onefile but still doesn't work,
2. Ensure the pygame is in the same directory as your .exe file
Apparently your .exe app is looking for the pygame module but failed. Try putting it in the same directory as your source code
There are multiple ways to convert a python file(.py) to an executable(.exe) -:
cx_Freeze
pyinstaller
auto-py-to-exe
Also, as mentioned in the comments by #Nanthakumar JJ and by #Amar Haiqal in his answer, make sure the --onefile option is checked while using auto-py-to-exe for conversion. Further, pygame module has to be present within the directory of the main file that is being compiled, as it will look for pygame and raise a ModuleNotFoundError, if the module is not found, being an .exe it will not look for the module on PYTHONPATH.
I have developed a simple software in python with GUI. I'm actually working on Natural Language Processing and I've just put the whole NLP process in a GUI.
I tried to convert the whole project to a exe file in python using Pyinstaller. I successfully converted it to an exe file but when I run it , it shows an error message something like this
This is the image of my error
I have already solved it but by using another way of converting py to exe which is the cx_Freeze.
I had the same issue earlier today and finally got it to work using the following software versions:
Python 3.6.8, nltk 3.5 and a dev version of pyinstaller:
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
Additionally, I used scikit-learn version 0.21.1.
Few days back, I had the same problem while compressing to EXE. This Problem generally occurs when PyInstaller failes to find Libraries and Modules to import to the directory. But I overcame this problem and the wise solution yet tedious method to do is mentioned below,
Convert Python Scripts to EXE with console = True in the .spec file or to be simple, do not add --noconsole argument while converting using System Argmuents method.(execute GUI with CMD)
After successfully building the file, go to respective directory (dist folder) and open command prompt
Run the Exe file using Command Prompt.
Find the error message and rectify it correspondingly. For example, consider the following Error Message, vcomp140.dll is missing from \\sklearn\\libs directory.
At the end of this Error Message, you'll find Failed to load dynlib/dll , Therefore, find the file or program which is missing. Say, Here we've .libs\\vcomp140.dll
Find vcomp140.dll using window search bar in your C Drive.
Copy the specific file and paste in the directory(under "dist" folder) where the file is missing. Here, the directory is dist\\PyScriptToEXE\\sklearn\\.libs
MatplotlibDeprecationWarning:
The MATPLOTLIBDATA environment variable was deprecated in Matplotlib 3.1 and will be removed in 3.3.
exec(bytecode, module.__dict__)
Traceback (most recent call last):
...
...
...
py3.7.egg\PyInstaller\loader\pyiboot01_bootstrap.py", line 169, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\MOHAMM~1\\AppData\\Local\\Temp\\_MEI38242\\sklearn\\.libs\\vcomp140.dll'. Most probably this dynlib/dll was not found when the application was frozen.
[13968] Failed to execute script try
Follow the steps again using CMD to eliminate each error.
I have download and installed Pyinstaller via pip with the goal of making an .exe binary that will be usable stand-alone on windows. My app/script is mostly similar to this one, downloading images from a certain subreddit (which is accessible without a reddit account).
Running the .py script via console or through my IDE works as expected, both on Linux and Windows. According to the PRAW documentation there has to be a C:\Users\myName\AppData\praw.ini config file post-PRAW's installation but it's currently missing from my system, yet, as I mentioned, the script runs just fine when on the python interpreter.
Packaging it into an .exe with Pyinstaller works fine but running it (with or w/o admin rights) fails with the following error
C:\Users\John\IdeaProjects\monsteraday-subreddit-image-downloader\dist>monsteraday-image-downloader.exe
Traceback (most recent call last):
File "<string>", line 5, in <module>
File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 363, in load_module
exec(bytecode, module.__dict__)
File "C:\Python27\lib\site-packages\praw\__init__.py", line 40, in <module>
from praw.settings import CONFIG
File "C:\Python27\Lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 363, in load_module
exec(bytecode, module.__dict__)
File "C:\Python27\lib\site-packages\praw\settings.py", line 47, in <module>
CONFIG = _load_configuration()
File "C:\Python27\lib\site-packages\praw\settings.py", line 45, in _load_configuration .format(locations))
Exception: Could not find config file in any of: [u'C:\\Users\\John\\AppData\\Local\\Temp\\_MEI52442\\praw\\praw.ini', u'C:\\Users\\John\\AppData\Roaming\\praw.ini', u'praw.ini']
monsteraday-image-downloader returned -1
At this point I should clarify that both
running python myscript.py in the console
executing the pyinstaller generated myscript.exe via console or double-clicking
Were tried on the same Win8.1 x64 machine, with all required libraries (including PRAW) installed, Python 2.7 32-bit (for compatibility of the target .exe) and the user praw.ini config missing from /AppData/ dir, while the global praw.ini properly in C:\Python27\Lib\site-packages\praw\praw.ini
Pyinstaller documentation mentions the usage of the .spec file to include external files in the wrapped .exe but I don't see how this play any role since I already lack/don't need the praw.ini configuration. Py2exe also produces the same results. How can I fix this and produce a straightforward way to use this tool?
Based on the last line of the output
Exception: Could not find config file in any of: [u'C:\\Users\\John\\AppData\\Local\\Temp\\_MEI52442\\praw\\praw.ini', u'C:\\Users\\John\\AppData\Roaming\\praw.ini', u'praw.ini']
It seems that PRAW, when run from a packaged .exe, is looking for its config file 'praw.ini' in 3 distinct locations
A temp folder created in the user's system directories, presumably
as instructed by pyinstaller
The user's Roaming folder (in Windows)
., in the same path the .exe is run from
Now, let's see what we can do to qualify for at least one of these cases.
It might be possible to tweak pyinstaller's settings and .spec file in a way that it takes a certain praw.ini file (like the global one found in Pytho's installed packages folder for PRAW) and places it in the temp folder so that the script can use but there doesn't seem to be an easy/direct way to do that. It definitely seems the most "proper" way to do it though.
Having each user place a config file there is bad for UX and should be avoided, unless of course there is an automated installation involved to do it, which might be overkill if we're talking about a small program.
The last is not ideal, but it's a decent compromise and definitely easy to roll with developer-side. All it takes is for the user to have the praw.ini in the same folder he/she is running the .exe from. The executable can be shared within a .zip archive containing a copy of the praw.ini as well, so that when run by the user it will be fully operational without extra effort on their behalf.
Even though it's actually a workaround, the last option will provide enough simplicity for the tool to be working out-of-the-box for an average user.
I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave