pudb debugging with tensorflow argv - python

Hi I am running through the /models-master/tutorials/image/cifar10 examples provided by google at https://github.com/tensorflow/models.
I am running the tensorflow-1.0.1 and python 3.5 in a virtual env.
From the command line, and in the virtualenv, running:
python3 cifar10_train.py
Works fine.
But when I try:
pudb3 cifar10_train.py
I get this error:
Traceback (most recent call last):
File "~/interpreters/p35/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 44, in run
_sys.exit(main(_sys.argv[:1] +
flags_passthrough))
TypeError: main() takes 0 positional arguments
but 1 was given
Checking the args gives:
print (_sys.argv[:1])
['cifar10_train.py']
print (flags_passthrough)
[ ]
I know pudb is in the same virtualenv that runs the code from the command line, as pudb goes through the tensorflow import fine, and the virtualenv is the only location in which the tensorflow package resides.
I am assuming this is some issue with passing between some layer pudb introduces... does anyone have a quick suggestion for getting through this... I just want to step through the code :)
Thanks,
nt

I've encountered this same problem when using pudb and TensorFlow. It has to do with the tf.flags. I just use python's Argparse class and not tf.flags.
As an alternative, I believe you can leave the tf.flags as is and just add from pudb import set_trace; set_trace() in your code after the tensorflow import. Call your script as python script.py wihtout -m pudb and it shouldn't freak out.

vega's suggestions worked... thanks. I'd rate up your comment but I've got a rep of <15... what else is new :)
So as per vega...
Included "from pudb import set_trace" after the "import tensorflow as tf" statement. Then added set_trace() as follows...
def main(argv=None): # pylint: disable=unused-argument
cifar10.maybe_download_and_extract()
if tf.gfile.Exists(FLAGS.train_dir):
tf.gfile.DeleteRecursively(FLAGS.train_dir)
tf.gfile.MakeDirs(FLAGS.train_dir)
train()
if __name__ == '__main__':
set_trace()
tf.app.run()
Called the script from the command line:
python3 cifar10_train.py
And it worked as desired.
PuDB looks like its going to be a great tool.

It seems that tensorflow is calling the wrong main function.I had a similar problem when using cProfile and calling script with
python -m cProfile train.py
Seems like the problem was that tf.app.run called main inside cProfile which wasn't ready for argument passing. In my case the solution was to specify main in tf.app.run():
tf.app.run(main=main)
Also fake argument in main is neeeded def main(_):.

Related

Running a module workaround python "Scalene"

I am trying to debug a memory leak in a module using Scalene.
Unfortunately, it appears that I can only run scalene script.py while I need to be able to specify the module to correctly run the application with python -m mymodule, which I can't seem to do with scalene.
Is there a way to overcome this? Thank you in advance
cf Scalene's documentation :
scalene your_prog.py # full profile (prints to console)
python3 -m scalene your_prog.py # equivalent alternative
You can use the second form with Scalene.
You can use runpy.run_module() to create a wrapper around your module, which you can then profile!
wrapper.py might contain:
from runpy import run_module
run_module('your_module_name', run_name='__main__')
and then you can run scalene wrapper.py!
The run_name argument is needed in order to "trick" the if __name__ == '__main__' clause into executing, if you have one.

Python Tornado AttributeError: module 'test' has no attribute '__path__'

I am attempting to just run the Hello World code from Tornado docs
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Except I am getting an error: AttributeError: module 'test' has no attribute '__path__'
I am just using IDLE to run test.py
I thought this was due to my Windows 10 computer not having Python accessible to PATH but even with adding in the python 3.6 to PATH I am still getting the same error. Any ideas?
The screenshot is how I added python to PATH and I think I got it correct..
------EDIT------
Ill add some screenshots of the errors/tracebacks I am running into. 1st one is the command prompt below when the test.py is ran in IDLE 3.6 in Windows 10.
If there is an import error, I can import Tornado just fine thru IDLE interpreter.
I also tried running this hello World code in IPython 3.7, and I get this error:
Solution: Run your file without the -m argument.
Another solution would be to provide the file name without the .py extension:
python -m test
This will also work.
Explanation:
The -m argument tells Python to run a module (file) present in the Python path. It doesn't take the name of the file, it takes the name of the module. The difference is that the file name contains .py suffix, whereas the module name doesn't.
So you can run the test.py file like this, too: python -m test.
When to use -m argument:
The -m argument is there for convenience. For example, if you want to run python's default http server (which comes with python), you'd write this command:
python -m http.server
This will start the http server for you. The convenience that -m argument gives you is that you can write this command from anywhere in your system and python will automatically look for the package called http in your the system's Path.
Without the -m argument, if you wanted to run the http server, you'd have to give it's full path like:
python C:\path\to\python\installation\http\server.py
So, -m argument makes it easy to run modules (files) present in the Path.
With Tornado would you happen to know how to kill the Python interpreter? A CNTRL-C doesn't do anything.
I use Linux and Ctrl-C works fine for me. On Windows you can try Ctrl-D or Ctrl-Z. Or here are some answers: Stopping python using ctrl+c

Executing a python script from scala giving error while running the same script from the command line does NOT

This is my python script:
!/usr/bin/python
import sys
import magic
m=magic.from_file('<file with absolute path goes here>')
print(m)
On running this from the command line:
$ python script.py
Microsoft Word 2007+
results in the output of the TYPE of the document that file is. Using python-magic.
Now running the same script from scala using below code:
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
throws the below error:
Traceback (most recent call last):
File "script.py", line 3, in <module>
import magic
ImportError: No module named magic
PS: I have both python 2.7 and python 3.6 installed on my machine and running the script using any of them from the command line runs just fine so I guess both of them are bundled with the MAGIC packages correctly.
Would highly appreciate any kind of help.
I will try to answer my own question in the best possible way.
While trying to execute the script.py from the command line using python compiler python2.7 was being used and when I tried to invoke the same script.py from the scala compiler using
import sys.process._
def compWithLibmagic(){
val result = "python /script.py" !
}
it was using python3 which was getting unnoticed. It took me a couple of days to actually figure this thing out and finally I removed all the python2.7 and python3 libraries and installed python 2.7 again and it worked like a charm.
It works from both python3 command and Scala code for me.
This should work.
import sys.process._
def compWithLibmagic(){
val result = "python3 script.py".!!
}

executing standalone fabric script by calling it by its name, without the .py extension

I have a fabric script called fwp.py that I run without calling it throug fab by using:
if __name__ == '__main__':
# imports for standalone mode only
import sys
import fabric.main
fabric.main.main(fabfile_locations=[__file__])
The thing is then have to call the script by calling fwp.py. I'd like to rename it as fwp to be able to call it as fwp. But doing that would result in
Fatal error: Couldn't find any fabfiles!
Is there a way to make Python/Fabric import this file, despite the lack of a ".py" extension?
To reiterate and clarify:
I'm not using the "fab" utility (e.g. as fab task task:parameter); just calling my script as fwp.py task task:parameter, and would like to be able to call it as fwp task task:parameter.
Update
It's not a duplicate of this question. The question is not "How to run a stand-alone fabric script?", but "How to do so, while having a script without a .py" extension.
EDIT: Original answer corrected
The fabric.main.main() function automatically adds .py to the end of supplied fabfile locations (see https://github.com/fabric/fabric/blob/master/fabric/main.py#L93). Unfortunately that function also uses Python's import machinery to load the file so it has to look like a module or package. Without reimplementing much of the fabric.main module I don't think it will be possible. You could try monkey-patching both fabric.main.find_fabfiles and fabric.main.load_fabfiles to make it work.
Origininal answer (wrong)
I can get this to work unaltered on a freshly installed fabric package. The following will execute with a filename fwp and executable permission on version 1.10.1, Python2.7. I would just try upgrading fabric.
#!/usr/bin/env python
from fabric.api import *
import fabric.main
def do():
local('echo "Hello World"')
if __name__ == '__main__':
fabric.main.main(fabfile_locations=[__file__])
Output:
$ ./fwp do
Hello World
Done

Unittest causing sys.exit()

No matter what I do sys.exit() is called by unittest, even the most trivial examples. I can't tell if my install is messed up or what is going on.
IDLE 1.2.2 ==== No Subprocess ====
>>> import unittest
>>>
>>> class Test(unittest.TestCase):
def testA(self):
a = 1
self.assertEqual(a,1)
>>> unittest.main()
option -n not recognized
Usage: idle.pyw [options] [test] [...]
Options:
-h, --help Show this message
-v, --verbose Verbose output
-q, --quiet Minimal output
Examples:
idle.pyw - run default set of tests
idle.pyw MyTestSuite - run suite 'MyTestSuite'
idle.pyw MyTestCase.testSomething - run MyTestCase.testSomething
idle.pyw MyTestCase - run all 'test*' test methods
in MyTestCase
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
unittest.main()
File "E:\Python25\lib\unittest.py", line 767, in __init__
self.parseArgs(argv)
File "E:\Python25\lib\unittest.py", line 796, in parseArgs
self.usageExit(msg)
File "E:\Python25\lib\unittest.py", line 773, in usageExit
sys.exit(2)
SystemExit: 2
>>>
In new Python 2.7 release, unittest.main() has a new argument.
If 'exit' is set to False, sys.exit() is not called during the execution of unittest.main().
Your example is exiting on my install too. I can make it execute the tests and stay within Python by changing
unittest.main()
to
unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromTestCase(Test))
More information is available here in the Python Library Reference.
Don't try to run unittest.main() from IDLE. It's trying to access sys.argv, and it's getting the args that IDLE was started with. Either run your tests in a different way from IDLE, or call unittest.main() in its own Python process.
Pop open the source code to unittest.py. unittest.main() is hard-coded to call sys.exit() after running all tests. Use TextTestRunner to run test suites from the prompt.
It's nice to be able to demonstrate that your tests work when first trying out the unittest module, and to know that you won't exit your Python shell. However, these solutions are version dependent.
Python 2.6
I'm using Python 2.6 at work, importing unittest2 as unittest (which is the unittest module supposedly found in Python 2.7).
The unittest.main(exit=False) doesn't work in Python 2.6's unittest2, while JoeSkora's solution does, and to reiterate it:
unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromTestCase(Test))
To break this down into its components and default arguments, with correct semantic names for the various composed objects:
import sys # sys.stderr is used in below default args
test_loader = unittest.TestLoader()
loaded_test_suite = test_loader.loadTestsFromTestCase(Test)
# Default args:
text_test_runner = unittest.TextTestRunner(stream=sys.stderr,
descriptions=True,
verbosity=1)
text_test_runner.run(loaded_test_suite)
Python 2.7 and 3
In Python 2.7 and higher, the following should work.
unittest.main(exit=False)
try:
sys.exit()
except SystemExit:
print('Simple as that, but you should really use a TestRunner instead')

Categories

Resources