Skip to content
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

Why Line Contents not being shown with line profiler? #152

Open
Jaya472 opened this issue Jun 23, 2022 · 13 comments
Open

Why Line Contents not being shown with line profiler? #152

Jaya472 opened this issue Jun 23, 2022 · 13 comments

Comments

@Jaya472
Copy link

Jaya472 commented Jun 23, 2022

I run my .ipynb file in the OneDrive folder via Anaconda JupyterLab. I am using %lprun line profiler and everything except for the Line Contents are displayed with the following message. What might be the mistake? Please, anyone, suggest. I am struggling for 10 hours just on this one thing!

I realize now maybe it is some version problem but don't know what versions I should have to solve this since I had already uninstalled and reinstalled the line profiler.

My python -V gives 3.9.7
My ipython -V gives 8.2.0
line_profiler (i see two versions in Anaconda environment) 3.5.1 and 3.4.0

Timer unit: 1e-07 s

Total time: 0.962042 s

Could not find file C:\Users\my name\AppData\Local\Temp\ipykernel_20788\3519952779.py
Are you sure you are running this program from the same directory
that you ran the profiler from?
Continuing without the function's contents.

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           
     2         1         24.0     24.0      0.0  
     3         6        211.0     35.2      0.0  
@Jaya472 Jaya472 changed the title Line Contents not being shown Why Line Contents not being shown with line profiler? Jun 23, 2022
@GoingMyWay
Copy link

Same issue, have you figured out the reason?

@Erotemic
Copy link
Member

Getting a MWE would go a long way towards fixing this.

@Jaya472
Copy link
Author

Jaya472 commented Jun 27, 2022

@Erotemic By MWE if you mean an example then I think I already gave one. Maybe, I can explain it. I have a very big code with many functions. At one such function say fn1, I use a line profiler command i.e. %lprun -f fn1 x=fn1(args) so that I know how much time the lines inside this function is taking. But when I run it I only get what is in my question. I later found out that maybe versions are the problem and so I mentioned the versions also in my question.

I had installed line profiler like 2-3 months ago and the same procedure of %lprun thing worked well. But now it is giving the error if I am running the program from the same directory as that of profiler. I am confused as I don't go and run the profiler separately, I only import it in my code and use it. I had also uninstalled and reinstalled the line profiler package but no use.

Ask me if you need any more specific details. I would like to get this problem solved as I will be coding in the next months as well.

@Erotemic
Copy link
Member

@Jaya472 Your MWE isn't complete because I don't have the ipynb file you are referencing. Please upload an ipynb file that demonstrates this error (e.g. as a github gist). Even better you could specify a simple set of commands that lets someone reproduce it via:

curl <url-of-your-notebook>
papermill notebook-name.ipynb output.ipynb

e.g. like this:

pip install papermill
curl https://raw.githubusercontent.com/Erotemic/xdoctest/main/testing/test_doctest_in_notebook.ipynb -o mwe.ipynb
papermill mwe.ipynb output.ipynb

@Jaya472
Copy link
Author

Jaya472 commented Jun 27, 2022

@Erotemic Oh ok. I never knew all this stuff. So, I now created a gist as you said and I am sharing the link here. I have created a small example to perform the sum of numbers and used the line profiler and showed the corresponding output. Hope this is enough or else let me know. https://gist.github.com/Jaya472/10805d3c20dea33908de372eee8d8e68

@Jaya472
Copy link
Author

Jaya472 commented Jun 27, 2022

@Erotemic And as you said about ipynb file, I created a private repository with the same code as I have put in the gist and this is the link. Hope this is what you were intending. https://github.com/Jaya472/an-mwe-for-line-profiler-problem/blob/f7d7f8c467f152851ede4f105e0b9d2f3b80ccd8/mwe.ipynb

@Jaya472
Copy link
Author

Jaya472 commented Jun 27, 2022

Same issue, have you figured out the reason?

@GoingMyWay Btw, as a workaround (for now) I have been able to solve the problem by running my code in Google Colab (the free version). Because as per the message by the compiler, it is a problem of some directory and installations. So I thought to do on google colab where it will take care of where to install line profiler and where to run the code. And it worked.

@Erotemic But of course, I would like to solve the problem for my personal workstation instead of every time using Colab resources. So please let me know if you need any more information or if you want me to try any resolving steps.

@Erotemic
Copy link
Member

So, I don't have access to your private repo, but with the gist you gave me I was able to cobble together the test. I believe you can just upload an ipynb as a public gist.

However, I'm not seeing this error. When I run I get this:

image

Perhaps this is a windows issue?

@Jaya472
Copy link
Author

Jaya472 commented Jun 27, 2022

@Erotemic Yes, that's what I was thinking that maybe it will work on your computer with no problem. Because if it is the directory problem or installation path problem or whatever it is, it would have been specific to my computer. But it is also true both my laptop and computer were giving me the same problem. Maybe something somewhere changed in the Windows update or pip install paths or even the jupyter notebooks' storage locations that there was some mismatch with the profiler.py unable to find the script on its path and causing the error in question.

Thanks for your help and for now, I will continue on google colab.

@Erotemic
Copy link
Member

I think it could have to do with this function in line_profiler.py:

# This could be in the ipython_extension submodule,
# but it doesn't depend on the IPython module so it's easier to just let it stay here.
def is_ipython_kernel_cell(filename):
    """ Return True if a filename corresponds to a Jupyter Notebook cell
    """
    return (
        filename.startswith('<ipython-input-') or
        filename.startswith(tempfile.gettempdir() + '/ipykernel_') or
        filename.startswith(tempfile.gettempdir() + '/xpython_')
    )

Could you insert a breakpoint or a print statement to determine what the filename is and if it is missing a case on your machine?

You can find the location line profiler is installed to via:

import line_profiler
print(line_profiler.__file__)

Just modify that file in an editor. I'm wondering if it is a backslash thing.

@amirebrahimi
Copy link
Contributor

Did anyone fine a workaround to this? Having this issue as well.

@amirebrahimi
Copy link
Contributor

After bisecting versions of ipykernel, I have found that the problem was introduced in version 6.7.0. I am using jupyterlab version 6.5.0 and line-profiler version 3.5.1.

I think the change is when long filename support was added to the temp paths:
ipython/ipykernel#838

I'm not sure if the issue is in ipykernel or how line-profiler is using it, but hope this helps others.

@Erotemic
Copy link
Member

Does this issue still exist in the latest release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants