Run code from a Python module, modify module, then run again without exiting interpeter - debugging

I'd like to be able to open a Python shell, execute some code defined in a module, then modify the module, then re-run it in the same shell without closing/reopening.
I've tried reimporting the functions/objects after modifying the script, and that doesn't work:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from my_module import buggy_function, test_input
>>> buggy_function(test_input)
wrong_value # Returns incorrect result
# Go to the editor, make some changes to the code and save them
# Thought reimporting might get the new version
>>> from my_module import buggy_function, test_input
>>> buggy_function(test_input)
wrong_value # Returns the same incorrect result
Clearly reimporting did not get me the 'new version' of the function.
In this case, it isn't that big a deal to close the interpreter and reopen it. But, if the code I'm testing is complicated enough, sometimes I have to do a fair amount of importing objects and defining dummy variables to make a context that can adequately test the code. It does get annoying to have to do this every time I make a change.
Anyone know how to "refresh" the module code within the Python interpeter?

use imp.reload():
In [1]: import imp
In [2]: print imp.reload.__doc__
reload(module) -> module
Reload the module. The module must have been successfully imported before.

Related

Why is python's pip install behaving strangely in a script but fine in python prompt?

I'm trying to install a module from withing a python script using pip. Here is the contents of script.py:
#/usr/bin/python2.7
# I'm the file called `script.py`
import sys, importlib, pip
print(sys.version); print(sys.path) # For debugging
try:
importlib.import_module('docopt')
except ImportError:
pip.main(['install', '-U', 'docopt'])
finally:
globals()[pack] = importlib.import_module('docopt')
Runnting this script, e.g. using python2.7 script.py gives me:
$ python2.7 script.py
2.7.9 (default, Oct 3 2016, 17:42:24)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)]
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages']
No module named docopt
Downloading/unpacking docopt
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement docopt
Cleaning up...
No distributions at all found for docopt
Storing debug log for failure in /root/.pip/pip.log
Traceback (most recent call last):
[...]
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named docopt
So problems with internet connection/ssl or something transport related. However, the same commands work perfectly fine in the python interactive interpreter, when I enter them by hand (copy&paste them):
$ python2.7
Python 2.7.9 (default, Oct 3 2016, 17:42:24)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip, sys
>>> print(sys.path)
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages']
>>> pip.main(['install', '-U', 'docopt'])
Downloading/unpacking docopt
Downloading docopt-0.6.2.tar.gz
Running setup.py (path:/tmp/pip_build_root/docopt/setup.py) egg_info for package docopt
Installing collected packages: docopt
Running setup.py install for docopt
Successfully installed docopt
Cleaning up...
0
Again, the thing that weirds me out is, that it works interactively i.e. running python2.7 in the shell and then entering the very same code by hand. However, not when I run the script file with python2.7 script.py. So In one case, on the same machine, the same interpreter has internet access in the other case it does not.
I'm running out of troubleshooting ideas. I used the same machine and user to get above results. There is no python-startup file, so that is not making the commands magically work interactively. The contents of sys.path are the same in both cases. I'm not behind a proxy. Any ideas what could be missing in the script?
Cannot fetch index base URL https://pypi.python.org/simple/
indicates that for some reason your python interpreter cannot access to internet. There can be plenty of reasons for that. Do you use a HTTP proxy ?

"Module use of python34.dll conflicts with this version of Python"

My knowledge of Python is still pretty basic, and I am only now trying to wrap my head around how to use / call libraries from within Maya. (Because I need to create a basic UI from QT Designer and have it be opened in Maya after converting it to a .py file)
After I learned to properly convert a .ui to a .py, I now get this error in Maya
"Module use of python34.dll conflicts with this version of Python"
I tried following what was said here and here, but even then - after setting these environment variables...
PYTHONHOME = C:\Program Files\Autodesk\Maya2016\bin\maya.exe
PYTHONPATH = C:\Python34
... I a still unable to run a basic .py file. In fact - as long as the PYTHONHOME variable is in effect, Python from within Maya no longer does anything.
This code below is the resulting python file that I got from converting the .ui file that I saved out of QT Designer.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
I've come across some different online notes that advised the use of PySide, and after trying to use:
from PySide.QtCore import *
from PySide.QtGui import *
I still couldn't test if this works, because apparently QtWidgets isn't a module?
I'm pretty lost. The bigger picture is that I just want to be able to run a ui created from QT Designer from Maya's script editor. I have no idea where to begin with learning about libraries. Would anyone here be kind enough to give some advice?
Additional info:
I am using Maya 2016, running on a Windows 10 64 bit OS.
I also have a Python 2.7 in my C: drive
And I am using PyQt5-5.4.1-gpl-Py3.4-Qt5.4.1-x64
Thank you for your time.
Maya's python interpreter is in the 2.7 series (or 2.6 for Maya 2013 and earlier). Your PYTHONPATH is pointing at a python 3.4 install. You also want to make sure that if your Python 3.4 is in PATH it comes later than the maya python install location. This is all to makes sure that Maya doesn't get confused and try to run python 3 code or dlls which it cannot handle.
Also, maya 2016 is running on PyQT4. Maya 2017 runs on PyQT5. So you'll probably want to use a PyQT4 version of designer
Related:
UIC / PyQT4 example
using UIC files in Maya pyQT 4
re: 2017/QT5

ipython can't load a module which python does

I have recently updated to OSX El Capitain upgrading an exhisting working osx installation. Now I have a module, installed via pip on an anaconda distribution which is now broken. Or better the module can be correctly imported from python
Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Sep 15 2015, 14:29:08)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import MDSplus as mds
whereas it does not load on ipython, claiming not to finding a library.
Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Sep 15 2015, 14:29:08)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: MacOSX
In [1]: import MDSplus as mds
Error importing MDSplus package: Error finding library: MdsShr
The library is correctly installed, it is listed in /etc/profile, it is located in /usr/local (so there should not be a problem with the new security system of OSX el Captain) and the python interpreted which is called via python or ipython is the same (Anaconda 2.3.0).
How can I diagnose which is the problem?
EDIT:
the solution proposed in https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/PEuOKEM5fdc does not worked for me. It does not seem to be an environment problem and the command
echo $DYLD_LIBRARY_PATH
points to the correct path
EDIT:
which -a ipython
ipython is /Users/vianello/anaconda/bin/ipython
which -a python
python is /Users/vianello/anaconda/bin/python
python is /usr/bin/python
>>> import sys
>>> for x in sys.path: print x
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/pyhht-0.0.1-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/MitDevices-0.3-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.157-py2.7.egg
>>>//anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.147-py2.7.egg/MDSplus
>>>/Users/vianello/anaconda/lib/python27.zip
>>>/Users/vianello/anaconda/lib/python2.7
>>>/Users/vianello/anaconda/lib/python2.7/plat-darwin
>>>/Users/vianello/anaconda/lib/python2.7/plat-mac
>>>/Users/vianello/anaconda/lib/python2.7/plat-mac/lib-scriptpackages
>>>/Users/vianello/anaconda/lib/python2.7/lib-tk
>>>/Users/vianello/anaconda/lib/python2.7/lib-old
>>>/Users/vianello/anaconda/lib/python2.7/lib-dynload
>>>/Users/vianello/anaconda/lib/python2.7/site-packages
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/PIL
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/aeosa
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/lmfit-0.8.3-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/setuptools-18.3.2-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/suds-0.4-py2.7.egg
in iPython the same give the results
/Users/vianello/anaconda/bin
/Users/vianello/anaconda/lib/python2.7/site-packages/pyhht-0.0.1-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/MitDevices-0.3-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.157-py2.7.egg
/Users/vianello/anaconda/lib/python27.zip
/Users/vianello/anaconda/lib/python2.7
/Users/vianello/anaconda/lib/python2.7/plat-darwin
/Users/vianello/anaconda/lib/python2.7/plat-mac
/Users/vianello/anaconda/lib/python2.7/plat-mac/lib-scriptpackages
/Users/vianello/anaconda/lib/python2.7/lib-tk
/Users/vianello/anaconda/lib/python2.7/lib-old
/Users/vianello/anaconda/lib/python2.7/lib-dynload
/Users/vianello/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/lmfit-0.8.3-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/setuptools-18.3.2-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/suds-0.4-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages
/Users/vianello/anaconda/lib/python2.7/site-packages/PIL
/Users/vianello/anaconda/lib/python2.7/site-packages/aeosa
/Users/vianello/anaconda/lib/python2.7/site-packages/IPython/extensions
/Users/vianello/.ipython
Thus the MDSplus-alpha_7.0.157-py2.7.egg can be seen both from python and ipython
My first thought is that you are running afoul of the new Apple System Integrity Protection. iPython is launched using a shell script that loads via /bin/bash. That will result in your DYLD_LIBRARY_PATH environment variable being stripped when python launches. You can check this by importing os and then attempting to print the contents of os.environ["DYLD_LIBRARY_PATH"]. If that print throws an exception then you know what the problem is.
A simple fix that works for me is to edit the first line of the ipython script so that the #! calls your python binary directly rather than going via bash. This is not a long term solution as it will have to be redone each time ipython is updated until upstream changes (to be fair I'm not sure why bash is involved).
I have written a report on python and library paths at http://dmtn-001.lsst.io

Numpy on osx 10.8.2 and python 3.2.3

What EXACTLY do I have to do to get numpy to work? I've read that it's supported in 3.2.3 and that it should work by using setup.py. I'm getting errors talking about os_path. Is there something I'm missing?
Here is the full traceback:
Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Converting to Python3 via 2to3...
Traceback (most recent call last):
File "/Users/cameron/Downloads/numpy-master/setup.py", line 214, in <module>
setup_package()
File "/Users/cameron/Downloads/numpy-master/setup.py", line 175, in setup_package
__file__ = os.path.join(os.curdir, os.path.basename(__file__))
NameError: global name '__file__' is not defined
>>>
You are probably using the Python version pre-installed with Mac OS X. I strongly suggest you use a distribution where all the components (python, scipy, numpy, etc) are built to work together properly, like macports. Macports is a package manager for Open Source software, and it automatically resolves and installs dependencies.
After the installation of Macports, the only thing you need to do is type "sudo port install py27-numpy" or "sudo port install py33-numpy" at the command prompt, depending on whether you prefer to use Python 2 or 3 (some packages might not run on Python3 yet). This will automatically download and install the correct version of python and all other software packages that might be required for numpy to run properly.
Try to compile a Python from scratch first (by downloading the source) and not using the one shipped by Apple. See the recommendation on the related SciPy/Numpy page.

How to enable the use of the gui resources on mac

I am having a problem with tkinter.ttk on mac. I am using macports and python3.1. When I try to use tkinter.ttk I get very old looking gui elements.
eg: I get this
Instead of this:
The code I used is:
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text="Hello World").grid()
root.mainloop()
I would be happy to provide any information from my computer needed to answer this question. As I am a novice programer please tell me where to find said information.
After a bit of digging I found this:
Python 3.1.2 (r312:79147, Jan 16 2011, 08:02:01) [GCC 4.2.1 (Apple
Inc. build 5664)] on darwin Type
"help", "copyright", "credits" or
"license" for more information.
->>> import tkinter.test.test_ttk.test_style
Traceback (most recent call last):
File "", line 1, in
File
"/opt/local/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/tkinter/test/test_ttk/test_style.py",
line 8, in
requires('gui') File "/opt/local/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/test/support.py",
line 215, in requires
raise ResourceDenied(msg) test.support.ResourceDenied: Use of
the gui' resource not enabled
->>> The error I have has something to do with the fact that
"Use of thegui' resource is not
enabled".
If two more people confirm that the code in blockquotes has nothing to do with the problem I will delete it from the question.
I have a Macbook 5,2 with Snow Leopard installed. Any help would be appreciated.
Thanks, Marlen
It's highly unlikely that this is related. You run the test from the Python command line, it needs to be enabled first, which is done by:
from test import support
support.use_resources = ['gui']
Then you can:
import tkinter.test.test_ttk.test_style
And see what it says (mine says nothing, but I'm on Ubuntu).

Resources