-
-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fails if dependencies include .pth files #381
Comments
NOTE: The |
Thanks for the thorough investigation and writeup! For background: That said - the remaining fixes all seem like (a) a good set of changes, and (b) not fundamentally incompatible with using a separate The fact that your local Python's path is being added to sys.path is definitely odd - and definitely something we want to avoid; the site path filtering seems like an interesting approach, although I guess the real fix is to work out why the extra path elements are leaking into Ideally, these changes wouldn't be something baked into the Briefcase sources either - they'd be something included in the briefcase template so that an end-user can easily customize the contents. |
Thanks for the feedback.
It's already there, in the current
Yet to clarify if this.
Understood and generally agreed (even though, as a side comment, I'd like to have I will investigate further and share my findings. |
InvestigationEnvironment:
Objective:
Starting Point: current
|
Stop and ThinkIssueFrom the experiments above, as soon as Explanation:
IssueUnderstand how/when Explanation:
...time passes ...code is read ...hacked with ...and print-debuged (is there a better way?) :-) Culprit:
StatusApparent Scenario
Possible ways forwardA. "Kind of ugly" option
B. "Might be nice but won't work" option
(this was a close one! oh, frustration!) C. "Not sure if its really that bad, but don't like it very much" option
(may limit updating the support package, like @freakboy3742 noted above -- then again, maybe not: support package isn't supposed to touch D. "What about a venv, which feels solid, but will probably be a mess" option.
E. "Is there any other option" option
:-) |
I had the same issue with packaging pywin32 in Briefcase and as @tmontes figured it out correctly, The python package This fix should be applied if any module does extra imports via a try:
import win32con, win32event, win32process
from win32com.shell.shell import ShellExecuteEx
from win32com.shell import shellcon
except ModuleNotFoundError:
print("Try to find 'app_packages' folder and to add this to python's 'site' package.")
app_packages = ""
for path in sys.path:
if path.endswith("app_packages"):
app_packages = path
if app_packages == "":
raise ModuleNotFoundError
else:
import site
site.USER_SITE = app_packages # correct the 'site-packages' path to 'app_packages' path
site.main() # recall site package thus all .pth in 'app_packages' will be add to sys.path
import win32con, win32event, win32process
from win32com.shell.shell import ShellExecuteEx
from win32com.shell import shellcon
for path in sys.path:
print(path) The output of the code above shows, that
@freakboy3742 : UPDATE: The following does not work because import sys
import site
app_packages = ""
for path in sys.path:
if path.endswith("app_packages"):
app_packages = path
if app_packages != "":
site.USER_SITE = app_packages # correct the 'site-packages' path to 'app_packages' path
site.main() # recall site package thus all .pth in 'app_packages' will be called |
@monzelr Thanks for the extra detail. I think this may be tracking the same problem as #669; and yes - this is absolutely something that should be fixed. The general approach you've described makes sense; we'll need to find a good place to drop a sitecustomize script so it is picked up on all platforms. |
#669 has a more direct test/manifestation of this; while the report here is windows specific, it's likely |
Context
As a Mu Editor contributor, and having worked a bit on improving its packaging, I joined the sprint today in order to understand how easy/difficult it will be for Mu to be packaged with the newer
briefcase
0.3 release.Facts about Mu packaging:
briefcase
0.2.x.pynsist
.wheel
+setuptools
.Challenges about Mu packaging:
setuptools
based and all of the information sourced fromsetup.py
+setup.cfg
(from there, we use a non-trivial script to produce the necessarypynsist
input such that it can do its thing, on Windows).The Issue
Packaging Mu on Windows leads to a partially ok Mu installation for various motives. In this issue, I'm focusing on a failure that results when trying to bring up its Python REPL -- it leads to a crash (Mu's fault) because a particular module fails to import, resulting in an unhandled exception.
Specifics:
qtconsole
that, on Windows, ends up requiringpywin32
.pywin32
uses.pth
files to guidesite.py
in populatingsys.path
in a specific way.Bottom line:
pywin32
fail to importwin32api
, on of its modules.Investigation
After a few hints from @freakboy3742 and @dgelessus at Gitter, here's what's going on:
pythonXX._pth
that:pythonXX._pth
file is actually overwritten bybriefcase
in order to:src\app
andsrc\app_packages
tosys.path
such that both the application and its dependencies can be successfully imported.._pth
file:site
module from being loaded at startup...sys.path
from any.pth
files that are found on thesite-packages
directories.A Successful Hack
With this information, I invested some time fiddling with these things to see if, at least, I could make it work. Here's what I did that resulted in a working Mu REPL (thus, its underlying
import win32api
working!):Hacked the cookiecutter template's
briefcase.toml
file (took me quite figuring out where this file was coming from!!!):app_packages_path
tosrc\python\lib\site-packages
, instead.Then, hacked
briefcase
's overwriting ofpythonXX._pth
to produce:site
imported such that.pth
files in the application dependencies are handled...Lastly, I observed that having
site
imported lead to an over-populated, non-safesys.path
. For some reason, my local Python installation'ssite-packages
was being added, and then maybe some more.With that, the last step of the hack, was creating a
sitecustomize.py
, which is automatically picked up whensite
is imported per the revampedpythonXX._pth
. Here's my take:With these three changes, the
import win32api
in Mu succeeds and, ultimately Mu works as intended WRT to providing a Python REPL.Thoughts
.pth
files in dependencies is a must, I'd venture saying.venv
to which dependencies arepip install
ed instead ofpip install --target
ed will be more robust, at least for the platforms where that is feasible. No worries about playing with import PATHs in three distinct places (well, maybe some PATH cleaning up could be in order, to guarantee isolation -- see mysitecustomize.py
, above).All in all, I leave this issue here in the hope that some useful discussion comes out of it, while serving as a hack-guide to someone facing a similar failures. I'm not sure yet about what could be a solid, sustainable, simple, future-proof solution.
Shall we discuss? :)
PS: I suppose similar failures will happen on any platform, as long as
.pth
files are brought in by dependencies.The text was updated successfully, but these errors were encountered: