How do you place a breakpoint inside a python package? - python

I'm working on contributing open source to the pandas package inside python. When I run import pandas as pd, it points to the installed version of python.
Wondering the best way to import the local version of the pandas library that I forked from github and set break-points inside to understand how the different functions work.

You should take a look at the pdb-module, using it for break-points goes as simple as:
import pdb # import the module
pdb.set_trace() # set break-point & open interactive shell
you can use the set_trace method whereever you want in any Python script you'd like to debug.
Here's a link to the official docs:
https://docs.python.org/3/library/pdb.html

Related

Check version of imported library in python script

in a python file, how to check the version of all the libraries that are imported exactly in that file by "import xxxxxxxxxxxx".
This is useful to later make an environment that has enough library to run that script while don't need to clone the exact environment.
example:
import pandas
import tensorflow
def version_list():
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# expect the version_list to return
# pandas==xxx tensorflow==xxx
# without returning other things
Many modules have their version set to the variable __version__. For example
import numpy
print(numpy.__version__)
prints the version of numpy which you have imported.
This may be related: Standard way to embed version into Python package?

How to know if all your script build in 3.8 works in 3.4 python

Short Question:
Is there a way to know the script works the same in 3.4,written in 3.8?
Long Question:
I wrote a script there uses a lot of if and else, when I build it in python 3.8.2, I have test each line myself. But now I need to use py2exe (which only supports till 3.4). So now I want to uninstall my python and install 3.4 but I'm not sure if all my codes will support in it. I started using python from version 3.8 only so I don't know what are the changes that took place from 3.4 to 3.8.2
these are my imports used
import pandas as pd
import eel
import bottle_websocket
import tkinter.filedialog
import xlrd
from configparser import ConfigParser
import os
import time
import xlsxwriter
import json
As I have used too much of if statements, I dont know if some lines inside a if will be executed or not.
You can maintain 2 different environments in the system.
So try running the code in both the environments and see if you get any warnings or errors.
Generally not much will change for the imports you have shared.

How to import site package's module instead of one in current directory that has the same name (python 3.x)?

I am heavy user of the pandas library.
In order to keep useful custom made helper functions related to pandas library, I decided to create a custom project (my_proj) and a module pandas.py in it.
Now I am developing another custom module related to ssh protocol in the same project.
Modules are created with pycharm. Structure of the project is as follows:
my_proj/src/my_proj/pandas.py
my_proj/src/my_proj/ssh.py
Everythin is OK and works properly. When I want to use site package's pandas I execute import pandas as pd, when I want to use my_proj pandas, than I use from my_proj import pandas as mypd.
But, now in ssh.py I need site package's pandas (not my_proj pandas).
If in ssh.py I use import pandas as pd, pycharm imports my_proj/src/my_proj/pandas.py instead of the pandas from site packages.
One solution would be to rename my_proj's pandas.py to something else, but I would like to avoid that if possible.
Is there another option to prevent loading library from current directory and import it from site packages?
What are my options?
At the end I will use the following procedure:
import sys
old_syspath = sys.path
sys.path = [path for path in sys.path if 'customspace' not in path]
import pandas as pd
sys.path = old_syspath
Basically, I am removing 'customspace' from sys.path, than importing pandas and puting sys.path back as it was.
I think this is minimal change and it is working.
I think that you could tell python exactly what directory to pull pandas from. EX from my_proj/src/my_proj/ssh import pandas.

Specify the developement version of a python module

I want to add a new class to PICOS, a python module. I installed it the normal way a long time ago. But now I have downloaded the source and I a am trying to make some changes.
The problem is that I cannot manage to ask python to load the module from the development folder and not the normal folder.
reload(picos.constraint)
Out[22]: <module 'picos.constraint' from '/home/optimi/bzffourn/python/lib/python2.7/site-packages/picos/constraint.pyc'>
while the source code are here:
/home/optimi/bzffourn/ZIB/python_scripts/pyMathProg/picos
So the changes I make are not considered.
This should help you do it: Override Python import order.
Just change your import to this:
import sys
sys.path.insert(0,"/home/optimi/bzffourn/ZIB/python_scripts/pyMathProg/picos")
import picos.constraint

Create documentation using pydoc and unknown modules

I'm afraid this will a question for a very particular case. At university we have been told to generate documentation by using pydoc. The problem is that we need to create it for a Maya script, and pydoc yells when it finds import maya.cmds as cmds
So, I tried to comment this line but I keep getting errors:
python C:\Python27\Lib\pydoc.py Script.py
problem in Script - <type 'exceptions.NameError'>: global name 'cmds' is not defined
I also tried Script, without the extension .py but it's silly doing that, we still running around the same issue.
Does anybody know how to generate documentation for Maya scripts, where import maya only works in the maya interpreter?
maya.commands is an stub module until it's run inside a working maya environment; if you just import it and inspect it outside of Maya you'll see that it's basically a placeholder.
If you want to inspect the contents, you can import the maya.standalone module and initialize it before running other commands (in this case it means you won't be able to run pydoc standalone.
You can get the documentation using the write command:
import maya.standalone
maya.standalone.initialize()
import pydoc
import mymodule
pydoc.write(mymodule) # writes the mymodule.html to current directory
Be warned, however, that the documentation for all maya built in functions will be unhelful:
'built-in function ls'
however you can at least document your own stuff without the maya parts crashing.
Pydoc, ironically, does not have a lot of external documentation. However you can see the code here:http://hg.python.org/cpython/file/2.7/Lib/pydoc.py (i'm not sure about the delta between this and the 2.6 version for maya pre-2014 but it works as above in maya 2011)

Categories

Resources