Function cannot open file after py2exe compile - python

I'm using rdkit.
After a build using py2exe, when I call the Draw.MolToImage method there is an error:
Warning: unable to load font metrics from dir
C:\pythonApp\dist\library.zip\rd
kit\sping\PIL\pilfonts
Traceback (most recent call last):
File "app.py", line 470, in <module>
img=Draw.MolToImage(part[i])
File "rdkit\Chem\Draw\__init__.pyc", line 124, in MolToImage
File "rdkit\Chem\Draw\MolDrawing.pyc", line 536, in AddMol
File "rdkit\Chem\Draw\MolDrawing.pyc", line 351, in _drawLabel
File "rdkit\Chem\Draw\spingCanvas.pyc", line 74, in addCanvasText
File "rdkit\sping\PIL\pidPIL.pyc", line 333, in drawString
ValueError: bad font: Font(12,0,0,0,'helvetica')`
There is no difference if I put these files into library.zip (to \rdkit\sping\PIL\pilfonts) or just into the dist folder and change paths in pidPIL.py to valid; the application still cannot open metrics.dat.
Without the py2exe conversion, everything works perfectly.

Are you using 'data_files' in your setup.py to include the pilfonts folder?
E.g. something along these lines:
setup(
data_files = [("pilfonts", glob.glob("\rdkit\sping\PIL\pilfonts\*.*"),
options = {"py2exe": {"compressed": 2,
# etc etc
)

Related

how to make pyomo lib portable?

I want to know how to make use of pyomo in my program which call the python interpreter to exe some pyomo scripts, but it shows that:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\LL\Desktop\face\face\Release\lib\site-packages\pyomo\environ\__init__.py", line 76, in <module>
_import_packages()
File "C:\Users\LL\Desktop\face\face\Release\lib\site-packages\pyomo\environ\__init__.py", line 73, in _import_packages
pkg.load()
File "C:\Users\LL\Desktop\face\face\Release\lib\site-packages\pyomo\opt\plugins\__init__.py", line 12, in load
import pyomo.opt.plugins.driver
File "C:\Users\LL\Desktop\face\face\Release\lib\site-packages\pyomo\opt\plugins\driver.py", line 14, in <module>
import pyomo.scripting.pyomo_parser
File "C:\Users\LL\Desktop\face\face\Release\lib\site-packages\pyomo\scripting\pyomo_parser.py", line 84, in <module>
description=doc, epilog=epilog, formatter_class=CustomHelpFormatter )
File "C:\Users\LL\Desktop\face\face\Release\lib\argparse.py", line 1647, in __init__
prog = _os.path.basename(_sys.argv[0])
IndexError: list index out of range
pyinstaller doesn't support secondary imports. You have to specify hidden imports of pyomo components in your .spec file

Create binary of spaCy with PyInstaller

I would like to create a binary of my python code, that contains spaCy.
# main.py
import spacy
import en_core_web_sm
def main() -> None:
nlp = spacy.load("en_core_web_sm")
# nlp = en_core_web_sm.load()
doc = nlp("This is an example")
print([(w.text, w.pos_) for w in doc])
if __name__ == "__main__":
main()
Besides my code, I created two PyInstaller-hooks, as described here
To create the binary I use the following command pyinstaller main.py --additional-hooks-dir=..
On the execution of the binary I get the following error message:
Traceback (most recent call last):
File "main.py", line 19, in <module>
main()
File "main.py", line 12, in main
nlp = spacy.load("en_core_web_sm")
File "spacy/__init__.py", line 47, in load
File "spacy/util.py", line 329, in load_model
OSError: [E050] Can't find model 'en_core_web_sm'. It doesn't seem to be a Python package or a valid path to a data directory.
If I use nlp = en_core_web_sm.load() instead if nlp = spacy.load("en_core_web_sm") to load the spacy model, I get the following error:
Traceback (most recent call last):
File "main.py", line 19, in <module>
main()
File "main.py", line 13, in main
nlp = en_core_web_sm.load()
File "en_core_web_sm/__init__.py", line 10, in load
File "spacy/util.py", line 514, in load_model_from_init_py
File "spacy/util.py", line 389, in load_model_from_path
File "spacy/util.py", line 426, in load_model_from_config
File "spacy/language.py", line 1662, in from_config
File "spacy/language.py", line 768, in add_pipe
File "spacy/language.py", line 659, in create_pipe
File "thinc/config.py", line 722, in resolve
File "thinc/config.py", line 771, in _make
File "thinc/config.py", line 826, in _fill
File "thinc/config.py", line 825, in _fill
File "thinc/config.py", line 1016, in make_promise_schema
File "spacy/util.py", line 137, in get
catalogue.RegistryError: [E893] Could not find function 'spacy.Tok2Vec.v1' in function registry 'architectures'. If you're using a custom function, make sure the code is available. If the function is provided by a third-party package, e.g. spacy-transformers, make sure the package is installed in your environment.
I had this same issue. After the error message you posted above, did you see an "Available names: ..." message? This message suggested that spacy.Tok2Vec.v2 was available but not v1. I was able to edit the config file for en_core_web_sm (for me at dist<name>\en_core_web_sm\en_core_web_sm-3.0.0\config.cfg) and change all references for spacy.Tok2Vec.v1 -> spacy.Tok2Vec.v2. I also had to do this for spacy.MaxoutWindowEncoder.v1. It's still a mystery to me as to why I'm having the issue only in the pyinstaller distributable and not my non-compiled script.
I encountered the same issue and nailed it by copying the spacy-legacy package to the compiled destination directory.
You can also hook it up by Pyinstaller but I did not really try that.
I hope my answer helps.

VSCode FileNotFoundError

I am trying to create a 2D arcade style game for a project in school using Python Arcade in VSCode, but I have issues correctly locating a map I created on a map editor.
Both the game code and map file are situated in the same main folder, with the map being placed in a sub-folder within the main folder.
I have used this line of code to specify the map location:
# Map name
map_name = ("maps/map1_level_{}.tmx.".format(level))
This is the error:
C:\Users\frag>python c:/Users/frag/Documents/game/arcade_game.py
Traceback (most recent call last):
File "c:/Users/frag/Documents/game/arcade_game.py", line 310, in <module>
main()
File "c:/Users/frag/Documents/game/arcade_game.py", line 305, in main
window.setup(window.level)
File "c:/Users/frag/Documents/game/arcade_game.py", line 120, in setup
my_map = arcade.tilemap.read_tmx(map_name)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arcade\tilemap.py", line 56, in read_tmx
tmx_file = resolve_resource_path(tmx_file)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\arcade\resources\__init__.py", line 30, in resolve_resource_path
raise FileNotFoundError(f"Cannot locate resource : {path}")
FileNotFoundError: Cannot locate resource : maps\map1_level_1.tmx
try this map_name = ("./maps/map1_level_{}.tmx.".format(level))
Fixed the issue by specifying the working absolute path:
import os
os.chdir('C:/Users/frag/Documents/game')

TypeError: expected str, bytes or os.PathLike object, not WindowsPath while converting .py using Pyinstaller

While trying to build an .exe using Pyinstaller, this error is thrown:
133235 INFO: Loading module hook 'hook-matplotlib.backends.py' from 'c:\\users\\jimit vaghela\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 901, in <module>
fail_on_error=True)
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 796, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 770, in _open_file_or_url
fname = os.path.expanduser(fname)
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\ntpath.py", line 291, in expanduser
path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not WindowsPath
134074 INFO: Loading module hook 'hook-matplotlib.py' from 'c:\\users\\jimit vaghela\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\PyInstaller\\hooks'...
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 901, in <module>
fail_on_error=True)
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 796, in _rc_params_in_file
with _open_file_or_url(fname) as fd:
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\site-packages\matplotlib\__init__.py", line 770, in _open_file_or_url
fname = os.path.expanduser(fname)
File "c:\users\jimit vaghela\appdata\local\programs\python\python37\lib\ntpath.py", line 291, in expanduser
path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not WindowsPath
I have found a solution posted on the Stackoverflow which states that there needs to be inserted a code into the backend.py in the Pyinstaller folder. But that does not work either.
What is going wrong here?
So, I found out that matplotlib was the issue. Excluding that in the module argument in Pyinstaller fixed it.
Like this:
pyinstaller --exclude-module matplotlib main.py
That is currently an issue with matplotlib.
I solved it by editing the source file. If you don't want to edit source file, it is said that lower versions of matplotlib like 3.0.3 can be installed to forgo this issue.
I my case it didn't work. Anyways, below are the steps I took.
First open Python interpreter & copy the path which you get as output.
>>> import matplotlib
>>> matplotlib.get_data_path() # copy the below path
'C:\\<Python Path>\\lib\\site-packages\\matplotlib\\mpl-data'
Next, open file C:\<Python Path>\lib\site-packages\PyInstaller\hooks\hook-matplotlib.py. Just to be on safe side make a back-up of this file if you need
from PyInstaller.utils.hooks import exec_statement
# old line; delete this
mpl_data_dir = exec_statement(
"import matplotlib; print(matplotlib.get_data_path())")
# Add this line
mpl_data_dir = 'C:\\<Python Path>\\lib\\site-packages\\matplotlib\\mpl-data'
assert mpl_data_dir, "Failed to determine matplotlib's data directory!"
datas = [
(mpl_data_dir, "matplotlib/mpl-data"),
]
And don't forget to save.

What does this Python message mean?

ho-fe3fdd00-12:~ Sam$ easy_install BeautifulSoup
Traceback (most recent call last):
File "/usr/bin/easy_install", line 8, in <module>
load_entry_point('setuptools==0.6c7', 'console_scripts', 'easy_install')()
File "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/setuptools/command/easy_install.py", line 1670, in main
with_ei_usage(lambda:
File "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/setuptools/command/easy_install.py", line 1659, in with_ei_usage
return f()
File "/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/setuptools/command/easy_install.py", line 1674, in <lambda>
distclass=DistributionWithoutHelpCommands, **kw
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/core.py", line 125, in setup
dist.parse_config_files()
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/distutils/dist.py", line 373, in parse_config_files
parser.read(filename)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ConfigParser.py", line 267, in read
self._read(fp, filename)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/ConfigParser.py", line 462, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: /Users/Sam/.pydistutils.cfg, line: 1
'install_lib = ~/Library/Python/$py_version_short/site-packages\n'
I am trying to install beautifulsoup.
The first two lines in ~/.pydistutils.cfg:
install_lib = ~/Library/Python/$py_version_short/site-packages
install_scripts = ~/bin
BeautifulSoup is a pure Python module which you can install by grabbing the BeautifulSoup.py file (eg. from inside the standard .tar.gz distribution) and putting it somewhere on your PythonPath - eg. inside /Users/Sam/Library/Python/2.5/site-packages, if the paths mentioned in the error message are accurate.
No need for fussy and error-prone installers which just overcomplicate the issue.
The configuration file .pydstutils.cfg has a syntax error.
Try to add the line at the top of ~/.pydistutils.cfg:
[easy_install]

Categories