pyinstaller importerror when module is in the same directory - python

Pyinstaller recently stopped importing a module that it previously had no problem with so I set up a simple test case to isolate the problem.
I created a script (pt.py) as follows:
import py_script1
import py_script
print('hello world')
py_script.py and py_script1.py are identical and located in the same directory as pt.py. They both import os, random and math and have some functions in them.
When running pyinstaller with default settings it compiles the .exe file but when I try to run it, I get "ImportError: No module named py_script". Checking the warnings text file shows that it was never compiled into the .exe file.
Changing the order of the imports doesn't help - I get the same error. I have also tried to uninstall and reinstall pyinstaller but that has not helped
How is this even possible - the two imported scripts are identical and are located in the same place!
More importantly how do I fix this problem?
PyInstaller 3.3.1
Python 3.3.5
Windows 10

I solved it. Despite the misleading exception description I isolated the problem as the following line in the imported file:
SB_CONSTANT = 0.00000005670373 #Stefan–Boltzmann constant (W/m2/K4)
For some reason this number broke pyinstaller. I changed it to:
SB_CONSTANT = 5.6704e-8
...and the module imported fine.

Related

Python Pyinstaller build fails using win32com package [duplicate]

I am trying to convert my .py file to an .exe file and I have tried all methods (auto-py-to-exe, pyinstaller, cz_freeze) and it does create the exe file but it always gives an error or the window opens and closes as soon as I double click the file.
It is a SpeechRecognition AI project coded in python. And it works just fine in the IDLE but once I create the .exe and try to run it the window pops up and shut down immediately after. (I use the cx_freeze and setup.py method for this)
If I try to convert .py to .exe using pyinstaller it gives me several different kinds of error messages.
As a .py file it works just fine but it doesn't work as an exe.
This is the error I get when using pyinstaller or auto-py-to-exe: Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path
Module 'pythoncom' isn't in frozen sys.path
I tried several things but nothing seems to work. I was previously using Python3.10 so I uninstalled it and downgraded to Python3.8 and reinstalled all the modules so technically it should work. I tried to create .exe files of another project and it worked just fine.
Another issue I come across is ModuleNotFoundError: No module named 'pyttsx3.drivers' I compiled the .exe using cx_freeze and it did create an .exe but it gives me this error.
Could someone please help me out with this?
(PS: This is the list of imports I am using for this project:
screenshot of all imports
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import wikipediaapi
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
from newsapi import NewsApiClient)
I just solved this for my project, which is using none of the direct imports your project is, but it appears the same low level library is causing an issue.
In my case, the single import is:
from pywinauto.application import Application
The solution is to tell PyInstaller to bundle the missing .DLL, which in this case is pythoncomxx.dll where xx is your python version. 39 in my case.
Here is the call that eventually worked for me.
pyinstaller --onefile --add-binary ".venv/Lib/site-packages/pywin32_system32/pythoncom39.dll;." autoTest.py
More generally: pyinstaller --onefile --add-binary "[path to dll];." file.py
Note, I'm on Windows. If you are on mac/linux, the ; character in the --add-binary argument would be :. See note in the documentation here
Discussion
This clicked for me when I used ProcMon to profile the file access of my initial version that was failing. It was looking for this .DLL in a bunch of different folders, then quitting.
The answers to this question were helpful. But I didn't want to settle for copying the DLL to output manually. And consequently, having to use a directory output versus a single executable file.
Answers on this question also gave the hint about using the --add-binary flag, but ultimately I had to provide the specific path to the missing DLL instead of just referencing by name as these answers showed. It probably works to specify the DLL by name if it is accessible on your PATH.
How to locate this DLL in the first place? Do a search on your site-packages folder.

Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path

I am trying to convert my .py file to an .exe file and I have tried all methods (auto-py-to-exe, pyinstaller, cz_freeze) and it does create the exe file but it always gives an error or the window opens and closes as soon as I double click the file.
It is a SpeechRecognition AI project coded in python. And it works just fine in the IDLE but once I create the .exe and try to run it the window pops up and shut down immediately after. (I use the cx_freeze and setup.py method for this)
If I try to convert .py to .exe using pyinstaller it gives me several different kinds of error messages.
As a .py file it works just fine but it doesn't work as an exe.
This is the error I get when using pyinstaller or auto-py-to-exe: Failed to execute script 'pyi_rth_win32comgenpy' due to unhandled exception: Module 'pythoncom' isn't in frozen sys.path
Module 'pythoncom' isn't in frozen sys.path
I tried several things but nothing seems to work. I was previously using Python3.10 so I uninstalled it and downgraded to Python3.8 and reinstalled all the modules so technically it should work. I tried to create .exe files of another project and it worked just fine.
Another issue I come across is ModuleNotFoundError: No module named 'pyttsx3.drivers' I compiled the .exe using cx_freeze and it did create an .exe but it gives me this error.
Could someone please help me out with this?
(PS: This is the list of imports I am using for this project:
screenshot of all imports
import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import wikipediaapi
import webbrowser
import os
import time
import subprocess
import wolframalpha
import json
import requests
from newsapi import NewsApiClient)
I just solved this for my project, which is using none of the direct imports your project is, but it appears the same low level library is causing an issue.
In my case, the single import is:
from pywinauto.application import Application
The solution is to tell PyInstaller to bundle the missing .DLL, which in this case is pythoncomxx.dll where xx is your python version. 39 in my case.
Here is the call that eventually worked for me.
pyinstaller --onefile --add-binary ".venv/Lib/site-packages/pywin32_system32/pythoncom39.dll;." autoTest.py
More generally: pyinstaller --onefile --add-binary "[path to dll];." file.py
Note, I'm on Windows. If you are on mac/linux, the ; character in the --add-binary argument would be :. See note in the documentation here
Discussion
This clicked for me when I used ProcMon to profile the file access of my initial version that was failing. It was looking for this .DLL in a bunch of different folders, then quitting.
The answers to this question were helpful. But I didn't want to settle for copying the DLL to output manually. And consequently, having to use a directory output versus a single executable file.
Answers on this question also gave the hint about using the --add-binary flag, but ultimately I had to provide the specific path to the missing DLL instead of just referencing by name as these answers showed. It probably works to specify the DLL by name if it is accessible on your PATH.
How to locate this DLL in the first place? Do a search on your site-packages folder.

import statement is not working when running python script from the command line

I need to run a python script from the command line (OS = Debian wheezy, python -version 3.5).
I used PyCharm (community edition) to write the script and it is working from inside the IDE.
I used sys.path.append command to add the directory containing the package I want, then followed it with this import line:
from package_name,file_name import ClassName
The Error message in the command line:
ImportError: No module named 'package_name'
if you are running any xxx.py file and you face the import error though same script if run by any IDE works fine,its path issue.
What worked fine for me is:
Go to file which shows import module issue and before importing module(for which issue is seen),add the path of module to sys using append.
for example ,I was running the script file from conf path and my script was importing module situated in \scripts\Setup\ so appended the path of module like below.
import sys
import os
conf_path = os.getcwd()
sys.path.append(conf_path)
sys.path.append(conf_path + '\scripts\Setup')
then use import statement of module for which issue was thrown.
I found the answer for my question above, and the problem was much easier than I thought.
Addressing the Problem
having many python packages in different directories
your script needs some/all packages, that are not in the standard lib-directory for your python installation (e.g.:prefix/lib/pythonVersion).
Solution
Short term solution
As long you are using an IDE (e.g. PyCharm), it is sufficient within the code to add:
import sys
sys.path.append("path/to/package")
As soon as you have to run your script from the command line, you will get an ImportError as mentioned in the Question above.
Better solution
Add the directories of your packages and of your python installation to your shell-profile(e.g.: .bashrc) using the command:
export PYTHONPATH=prefix/lib/pythonVersion:/path/to/packages
To get more info about PYTHONPATH, check this link
In this case you will not need to append the path of your packages within your code :)

WinPython: ImportError: No module named %name%

I installed WinPython-64bit-3.5.1.2
I installed it not into default directory C:\Python, but into a different one like C:\"....My path...."\WinPython-64bit-3.5.1.2.
I downloaded a specific python project with all of its files and modules and placed this folder in Python folder so it has path like: C:\"....My path...."\WinPython-64bit-3.5.1.2\project
I added this path to the system path like this:
import sys
sys.path.append("C:\"....My path...."\WinPython-64bit-3.5.1.2\project")
There is an setup.py file in the project folder which I can't seem to run - whenever i type python setup.py I get an error: SyntaxError: invalid syntax
Whenever I try to import some module from this project I get the message ImportError: No module named %module_name%
I tried this both from Spyder and Ipython console.
I've read some stuff about system variables and their altering, however I do not seem to have "pythonpath" variable defined, do i need to define it? What do I put in it?
Its weird, because yesterday I was able to import the exact same module, but today I already can't. I couldn't find out what is the problem here and don't seem to remeber the exact steps I performed the day before. So am asking for qualified help. This is my first time using Python.
Thanks!
suppose there is a Project\setup.py
launch WinPython Command Prompt.exe
cd/D "C:\"....My path...."\WinPython-64bit-3.5.1.2\project"
python setup.py install
and, then, to check it's there:
pip list

Why is my python installation not working properly?

http://pastebin.com/BJiXC022
At first my python is working just fine with tkinter. When I change the working directory, it somehow stops working then. It even manages to refer the tkinter.py file in that directory even when I never even typed the name of the file there. I just wanted to import tkinter. My tkinter.py file is also not working even though it is almost exactly the same as the first 10 lines. How do I fix this problem? I reinstalled os and python yesterday, I am running OS X 10.10.3 and the newest Python 3.4.3. Here's tkinter.py:
http://pastebin.com/VBHqFGLZ
You have a file named tkinter.py in /Users/nikolas/Documents/Python/tkinter.py. Changing to that directory and importing tkinter will import the local file, not the one from your Python installation. You see the error because your tkinter.py file does not provide Tk.
The solution is to rename your file to something other than tkinter.py.

Categories