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

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

Related

Trouble setting up libraries imported from github

I'm trying to clone a repo to my machine to test changes for a pull request.
The repo in question is a clone of pytorch, and I want to add something to one of the files to fix an issue. I figured how to clone the repo, but I can't figure out how to import the pytorch libraries when I write a test file that contains something like:
import torch
x = torch.rand(5, 3)
print(x)
Where am I supposed to create a test.py file? How do I add pytorch (specifically my cloned version of pytorch) to the list of dependencies for Python to run with? I tried just creating a test.py file at the same level as the cloned repo, but i get the error message
"no module named torch.version". I am using VS code.
I'm new to using git and not extremely familiar with the structure of libraries like this. I tried looking through github, stack overflow and the pytorch docs but was unable to find an explanation.
Make sure your current selected interpreter doesn't contain pytorch, then before import torch, add the following code:
import sys
sys.path.append("\the folder that contains pytorch\")
Please have a try.

What is the simplest possible package I could make and run?

I am struggling a bit to get a package to work, so I would like to just get the simplest possible test case to work.
Here is my current attempt.
Inside of a folder called Python_experiment I have two files: a jupyter notebook with the code
from .pleasejustwork import eat_muffin
and a file called pleasejustwork.py with the code
def eat_muffin():
print('i ate a muffin')
When I run the line from the jupyter notebook I get the error "attempted relative import with no known parent package". What am I missing?
The syntax you’re using is for a python package. To achieve this, place a file, __init__.py in the directory. It can be empty. If you’d like to use that package from anywhere, write a setup.py script to build and install your package. There are a lot of good tutorials on how do that, like in the python docs. Then you could do
from python_experiment.pleasejustwork import eatmuffin or from another file in the package from .pleasejustwork import eatmuffin
If you’re just trying to learn about modules, you can simply take out that leading period
from pleasejustwork import eatmuffin

How to import .Net Classes in Python using Python.Net

I'm using Python.NET to create wrapper for iText.net (https://github.com/itext/itext7-dotnet).
The dll is named itext.kernel.dll and the python script is in the samne folder of all the itext dlls.
With Jetbrain dotPeek I see that itext.kernel has various namespaces; the namespace iText.Kernel.Pdf is what I need, in particular the public class PdfReader.
Here's what I'm doing (from the docs on https://pythonnet.github.io/):
import clr
clr.AddReference('itext.kernel')
sys.path.append(os.getcwd()) # the script is currently in the dll folder
from iText.Kernel.Pdf import PdfReader
But this gets me a "ModuleNotFoundError: No module named 'iText'" error.
I can't understand what it's wrong, can someone point me to the right direction? Thanks.
edit: I can import and use iTextSharp just fine, it seems iText7 has something different.
Well, long story short I really wanted this to work... and solved my issue!
I tried using older builds, and I found out that build 7.0.8 worked fine.
I compared it with build 7.1.11 (the one I was having problems with) using dotPeek, and noticed it has some references, namely BouncyCastle.Crypto, Common.Logging and Common.Logging.Core.
I did put them in the project folder, but still no dice... until I noticed I was using BouncyCastle.Crypto build 1.8.6, while the itext.kernel referred build 1.8.5! Replacing with that version solved the issue.
Basically the newer builds of iText7 have 3 referenced libs, that must be present in the exact same version (at least to be imoported by pythonnet).
I hope this can help someone that has issues similar to mine.

Is it possible to specify the search path for a module in a python script? If it is, how do I do that?

I have been coding in python for about 2 months, but I'm only familiar with basic object-oriented programming, so I do not really understand things like how searching for modules is implemented. (Basically I'm a noob.)
I pip installed a package called Opentrons Opentrons 2.5.2 and all its dependencies into the samefolder as a python script I'm currently writing. However when I tried to import the module below[1], I get an error saying that "Opentrons is not a module". Then, I tried shifting it into the python library because I found out the search path using the pprint module and it seems to work. I was wondering if I can specify the search path from the .py file itself instead of manually printing the search path and putting the file into the library that the script searches for. (Willing to put in images of the directories I put the opentrons package in if it helps.)
[1]
import sys
import pprint
pprint.pprint(search.path)
from opentrons import robot, containers, instruments
Edit: I realise that the fact that I am running all my scripts in a Spyder console located in a python 3.6 environment might be important.
You can try using the __import__ function, or importlib. This should allow you to specify the path.

How can I access to library " plot_utils " in python?

I need this library "plot_utils" for analysis and plotting but I can't find it in the web. this library is related to Brian2 (a library in python) for spiking neural network.
I installed brian2 through anaconda. when I want to import plot_utils and using in my code I receive error :
ModuleNotFoundError: No module named 'plot_utils'
Could you please help me?
You need to give more info about where did you stumble upon that name, eg some copied code etc.
With the only reference you've given (ie. brian2) this seems related.
https://brian2.readthedocs.io/en/stable/examples/frompapers.Stimberg_et_al_2018.plot_utils.html
Maybe just copy that code into a file named 'plot_utils.py' and keep it at the path your code is searching for it.
First, you need to install the module in your work environment using "pip install plot_utils" then you can import the library using "import plot_utils".
This link will help you: https://libraries.io/pypi/plot-utils[this is the official documentation from plot_utils]

Categories