How to get quick documentation working with PyCharm and Pytorch - python

I'm running PyCharm on Windows 10, and installed PyTorch following the getting started guide. Where I used Chocolatey and Anaconda to set up everything.
I can run the PyTorch tutorials from inside the PyCharm IDE without any problems. So I feel like I have a proper set up, but there aren't any intellisense documentations for any of the PyTorch APIs.
For example;
import torch
x = torch.randn(128, 20)
If I mouse over randn and press CTRL+Q then PyCharm shows me a popup of the function definition without any documentation.
I'm expecting to see Python comments from the API documentation for that function:
https://pytorch.org/docs/stable/torch.html?highlight=randn#torch.randn
I'm a new beginner with Pytorch and Python, but this is something that I often have access to from inside the IDE with many other languages and libraries. So I feel like this should be possible to get working, but I can't seem to find any instructions on how to fix this.

I was able to get it working by doing the following:
PyStorm 2019.3
Open the settings for external documentation:
File / Settings / Tools / External Documentation
Add the following URL patterns:
Module Name: torch.nn.functional
URL: https://pytorch.org/docs/stable/nn.functional.html#{element.qname}
Module Name: torch
URL: https://pytorch.org/docs/stable/{module.basename}.html#{element.qname}
Seems to work for most APIs but you have to trigger the quick documentation tool window. This won't show docs if you CTRL+CLICK something.

You probably want to get Kite. It has this feature (that I use intensively) called co-pilot where you can get python docs immediately. I don't know if it's Linux/Mac specific but works for any editor, nit just Pycharm.
It's just another solution

PyCharm2022.1.x still cannot display torch(1.10) documentation.
#Reactgular 's answer is helpful. But the url that works for me is different.
Following the steps:
file --settings--tools -- external documentation -- add
Module Name: torch
URL/Path Pattern: https://pytorch.org/docs/stable/generated/{element.qname}.html#{element.qname}
Actually the above steps only let the pycharm show a link. But it is better than nothing. just click the link and a default browser will open the documentation.
You can also specify a customized browser instead of the system default browser: file-- settings-- tools --web browsers and preview
torch quick doc

I was about just to add a comment for Reactgular's solution but it turned out I didn't have enough "reputation" to do it.
For the torch.nn.functional module, I found it is more helpful to do this to jump directly to the detailed documentation for the function you are searching for.
torch.nn.functional
https://pytorch.org/docs/stable/generated/{element.qname}.html#{element.qname}
But of course, this is not a stable page so I guess it may not work later, so be careful about it.

Related

Programmatically Execute Cell Jupyter VSCode

I am looking for a way to programmatically replicate the Run Cell Below functionality VS code.
Previously, I used Jupyter through Conda and used the following code:
import ipywidgets as widgets
from IPython.display import display,Markdown,Javascript,HTML
def run_below(ev):
Javascript('IPython.notebook.execute_cells_below()')
button = widgets.Button(description="Click to run cells below")
button.on_click(run_below)
display(button)
This code worked great, but when I tried to plop it into VSCode, the button just does nothing. I don't understand much about how the VSCode Jupyter backend works, but I'm imagining it has something do do with the IPython.notebook module not working correctly in this IDE (or perhaps the IPython.display.Javascript module?). I really have no real idea though.
Does anyone know how I could do this in VSCode's Jupyter implementation?
I have searched for hours on this topic, but have not been able to find a working solution that works. Please let me know if y'all have any ideas.
Environment Info:
Python Version: 3.9.12
VSCode Version: 1.69.0
Jupyter Extension Version: v2022.6.1001902341
It appears that the ability to access the Kernel in VS code is not possible at this time. See the following GitHub issues to see if this has changed at the time of reading:
Similar question migrated to #6918
Issue #6918 that will resolve problem once closed
Not exactly an answer to detailed question but is an answer to the title of the question "Programmatically execute cell jupyter vscode" since I landed on this page searching for how to do this. The following code supports this task - just make sure to hit save CTRL-S if you make changes to cells that are going to be run by the following function since it reads the current version of file from disk
def execute_cell(filepath,cell_number_range=[0]):
import io
from nbformat import current
with io.open(filepath) as f:
nb = current.read(f, 'json')
ip = get_ipython()
for cell_number in cell_number_range:
cell=nb.worksheets[0].cells[cell_number]
#print (cell)
if cell.cell_type == 'code' : ip.run_cell(cell.input)
also for finding name of current notebook in vscode is easy but not easy in jupyterlab
import os
globals()['__vsc_ipynb_file__'] #full name only in vscode
os.path.basename(globals()['__vsc_ipynb_file__']) #basename only in vscode . globals()['_dh'] #dir in most

Create a mess I need to clean up, while trying to solve conflicts between Macbook Pro M1 and Tensorflow library

I have a 2021 Macbook pro M1.
It is a well recognized problem that it conflicts with Tensoflow library, see here or here; in particular this last one was exactly the issue I experienced: when I tried to import tensorflow in jupyter notebook via the command
import tensorflow as tf
then the message
The kernel appears to have died. It will restart automatically.
appeared. Then searching in the above linked discussions, I have the feeling that the suggestion given at some point, which points at this link, SEEMS to be useful.
FIRST QUESTION: is this a/the solution for the M1-Tensorflow conflict?
I say "it seems" since before trying that I have been into the kind of tornado of desperate attempts leading a beginner like me to search for hints all around the web and then copy-paste commands taken here and there into the Terminal without understanding them all properly.
On one hand it sounds dumb, I admit, on the other the cost of understanding everything goes well beyond my humble intentions of learning some ML.
So, the final result is that I have a complete mess in my computer; the old libraries like numpy don't work anymore (when I import them inside a Python3 page opened with jupyter notebook with the command import numpy as np, the message
ModuleNotFoundError: No module named 'numpy'
appears), then the pip command doesn't work, if I use the pip3 to install, nothing changes. I read somewhere to use a virtual enviroment, and I followed the instructions even if I wasn't really aware of what I was doing; I downloaded XCode, miniforge3...
Well, I guess that there is somebody out there who can relate with this.
SECOND PROBLEM: I would like to clean-up everything dealing with Python/pip/anaconda and so on and install everything from scratch, possibly following the above link to solve the M1-tensorflow conflict...if it is correct. How can I do that?
Can somebody help me, please? Thanks

tensorboard: command not found, when I use

I already use python IDE PyCharm and have installed the package tensorflow but when I use the terminal and give an input to the tensor board as follows
--logdir = '/user/....',
it shows command not found?
is there still something Im missing like setting the tenor flow path or any thing else?
Maybe you need to activate your relevant environment via source activate some_conda_env.
If it isn't either of those you're going to need to give more information than you've given in the question (versions, environment, etc.)
Update (due to discussion in comments):
It seems that tensorboard is not in your search path.
That can be fixed, but meanwhile you can just run tensorboard by using the full path. e.g. /my/full/path/to/tensorboard --logdir='/path/to/logger' etc.
Good luck!

How to find a class or function in a Python module?

I'm trying to find the function or class definition of gen_dataset_ops in tensorflow, which has its sourcecode here. I find many places where it is imported like so:
from tensorflow.python.ops import gen_dataset_ops
But I can't find where it is defined, I'd expect to find something like:
def gen_dataset_ops(...):
#Do something clever
return
I don't quite understand the anatomy of python modules in general, so I'm probably missing some basics here,.. any hint is welcome!
tensorflow.python.ops.gen_dataset_ops is generated code. (That's why they put gen in front of the name.) You can't find it in the source repository because it's not in the source repository; it only comes into existence during the Tensorflow build process.
If you have Tensorflow installed, you should be able to find gen_dataset_ops.py under tensorflow/python/ops/gen_dataset_ops.py in your Tensorflow installation.
I would navigate to your python directory (this example is for a virtualenv on ubuntu):
~/pyEnvs/env1/lib/python2.7/site-packages/tensorflow/python/ops.py and open that file. In that file, use Ctrl+F and find the function you are looking for.
My answer assumes you know where your python environment is installed and that you installed tensorflow using pip

python's save figure button does not work in mac, trying to solve it unsuccessfully, how to do this?

I updated my python distribution yesterday to EPD 7.3-2 (64-bit). I am working on a mac with snow leopard.
Now the plot device of matplotlib is broken in at least two ways:
the "save" button doesn't work and makes the terminal or ipython crash and
the only way to see the figure is to have it in front of you, there is no python figure icon in the dock.
I did my homework and these same problems were reported here and here.
I tried to follow the instructions to fix this given in here, but this is the error that I get:
$python install_pythonw.py `which python`/../..
/Library/Frameworks/EPD64.framework/Versions/Current/.Python does not exist; exiting.
Indeed, I looked at the given folder and I could not find a .Python file. I added a comment at the answer to this problem but so far no one has replied to it :( :(
Any idea of how to fix this?
thanks!
I have seen this problem a few times, and it seems to be a problem in some backends. Also, it doesn't seem normal that your session crashes after 4 or 5 plots. In particular, the MacOSX backend seems buggy.
As you installed the EPD, I think it's less likely that your installation is broken.
The solution seems to be using a different backend. You can try with ipython --pylab a few backends, try their features and see if the save button works. You can try the following:
ipython --pylab=wx
ipython --pylab=tk
ipython --pylab=osx
The last one is the option that you're probably using right now, so perhaps not the best. If you just call ipython --pylab, it will use the default backend from your ~/.matplotlib/matplotlibrc file. Once you find a working backend you can change the default by editing that file. Look for a line like this:
backend : MacOSX
(your version may have a different backend.) Just change that setting to WXAgg, TkAgg, or Qt4Agg. With the --pylab option the names are slightly different, they don't have the Agg part. My favourite backend for OSX is the Qt4Agg backend, but I don't think it ships with EPD and the save button also doesn't work! But either WXAgg or TkAgg should work fine.
Other ways of changing the backend in a script are:
import matplotlib
matplotlib.use('WXAgg')
or
matplotlib.rcParams['backend'] = 'WXAgg'

Categories