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
Inspect fails to retrieve source code inside frozen app (.py files included) #4764
Comments
This is weird... Do you have the time to track the line that raises the |
Ah I was wrong. The problem already occurs in resolving the right filepath: I stepped through the
So already |
@lukasberbuer |
@Legorooj: I think the modules filename The
So Still, I wonder why the |
I'm not actually sure - but it's likely something to do with the python import system. I'll try and trace this. |
Do you have some ideas what I can do to narrow down the problem? I'm stuck at the moment... |
@lukasberbuer yes - traced the area of pyinstaller. Can you read C? You'll need it to trawl through the bootloader code - I've not had time yet. Sooo many of the issues that get opened on this repo are "please read the docs" etc. Or could be a bug - but the author's primary language isn't english, and the grammar is impossible to understand. So this issue got buried - it's on my to-do list... Somewhere. It'd be nice to get this fixed. |
@Legorooj: Sorry for the late response - was a busy week. I really appreciate your work as maintainers of such big project. |
@lukasberbuer before you do that, had another idea. In |
@lukasberbuer Well done tracking this down :-) As the code says, the source file is search in So the solution is to add the source-files to the PYZ in the .spec-file. PyInstaller currently does not provide support-functions for this, so you need to build the correct data-structures manually. Of course Scratch:
|
@lukasberbuer Uh, sorry, I was wrong - did not read the doc-string you copied. In addition to the above you would need to change the loader along these lines:
Please let us know how this works for you. |
@htgoebel: Great, that works!
Modified
Is there any way to (monkey)patch the |
@lukasberbuer just to check, can you run the tests on that mod? If it all works, a PR would be nice! |
@Legorooj: I'll work on that.. the f-strings limit it to py3.6 and above at the moment. |
@lukasberbuer
|
I wonder if this could more easily be solved by calling |
@bjones1 this was already tried; see the original comment. |
That would be wonderful. I need a way to make this thing work with hooks for |
@bjones1 @lukasberbuer idea:
|
@Legorooj: Yeah, this would be the nicest solution. The problem if was facing is that
Is there any way |
@Legorooj: I was wrong. Accessing the Imagine we have a |
Hmm. How about |
So I'm having the same problem as mentioned in #4815. I've tried adding the collect_source_files function to my spec file as shown above but still have not had success. Is there something else that I need to do? Here is my spec file:
|
@dwight9339: You need to modifiy the pyinstaller
|
@lukasberbuer Your solution works for me. I feel like this should be merged into the main branch. Is this code able to live in the source somewhere so that it can always read the actual source code of a frozen .py file?
|
@lukasberbuer do you need to rebuild pyinstaller after modifying |
Nevermind, it worked.
|
@PATAPOsha: I just pulled the PyInstaller repository, changed the few lines of code and installed it in the project in editable mode with @Alex-Mann: I would like to have it in the release as well but couldn't figure out a handy integration without diving to deep into the pyinstaller source code (as discussed above). The solution should work with hook files as well. In my opinion, we should have:
Would you agree on those implementation steps? |
@bwoodsend can you give a hand here? |
I've never worked with that corner of PyInstaller but surely just (5) I believe would simply be: diff --git a/PyInstaller/building/templates.py b/PyInstaller/building/templates.py
index 20bc31e2..35a4d678 100644
--- a/PyInstaller/building/templates.py
+++ b/PyInstaller/building/templates.py
@@ -30,7 +30,8 @@
cipher=block_cipher,
noarchive=%(noarchive)s)
pyz = PYZ(a.pure, a.zipped_data,
- cipher=block_cipher)
+ cipher=block_cipher,
+ sources=a.sources)
exe = EXE(pyz,
a.scripts,
a.binaries,
@@ -63,7 +64,8 @@
cipher=block_cipher,
noarchive=%(noarchive)s)
pyz = PYZ(a.pure, a.zipped_data,
- cipher=block_cipher)
+ cipher=block_cipher,
+ sources=a.sources)
exe = EXE(pyz,
a.scripts,
%(options)s, You'll also need a |
I'm trying to figure out a way to pack an application depending on pytorch with PyInstaller, and I'm lost. Is there a way to do this with the current PyInstaller release? I tried this workaround but that just gave me a new error due to |
#5697 resolves this. With |
Although I guess it would now make sense to add that |
I have a similar problem to the original poster, in that my app needs to be able to use Option 1: Do exactly what @lukasberbuer originally wanted to do. This method is simple and is particularly useful if you want to include all the source files from a package. This now works as requested due to the update in #5697. In the example below, I've tweaked the
Option 2: Collect source code for particular modules. This option is useful if you only need to include source code for one or a few specific modules. This is similar to the
For my app, both of these create an additional copy of the source code in Also note that PyInstaller automatically puts packages and modules from the Python standard library in the root of the packaged app, which is where |
This should not be the case; we do not collect source code by default, and if we did, we would do so into top-level application directory (e.g., due to module collection mode that was added in #6945). So if you have source code collected in By the same token, we do not add |
Maybe this is a Mac-specific issue? On my Mac, running PyInstaller 5.6.2 under miniconda, the following produces a
|
More likely an anaconda-specific issue, then (or an anaconda + macOS one). Our anaconda support is rather flaky and lacks thorough testing; could be the same sort of duplication problem as observed in #7165 (comment). Either way, I suggest you to switch to python.org + pip + venv for PyInstaller builds. |
Description of the issue
I'm trying to use
inspect.getsource(<object>)
, e.g.inspect.getsource(datetime.date)
in a frozen application.Although I manually added the source files to datas
the application crashes with
OSError: could not get source code
.The file path seems to get extracted correctly withinspect.getsourcefile(datetime.date)
though.Edit:
inspect.getsourcefile(datetime.date)
returns a non-existing file path[...]\dist\main\datetime.py
. It should be[...]\dist\main\lib\datetime.py
instead.All files to reproduce the problem are found here:
https://gist.github.com/lukasberbuer/d9cc1716ada09f54c53640a97a11f7a5
Context information (for bug reports)
pyinstaller --version
:3.6
The text was updated successfully, but these errors were encountered: