Syntax error while using python trace - python-2.7

I'm running a script of which I need to see which statements of whichever files it touches. So, I'm using Python's trace.
This command runs perfectly on the linux terminal: /x/eng/bbsvl/users/nikhilh/C4563855_4563855_1710110159/test/nate/bin/ntest -noconsole NATE_HOSTS=/u/smoke/presub/tharnhosts/temp_remove/nikhilh NODES=p8020-5 /x/eng/bbsvl/users/nikhilh/C4563855_4563855_1710110159/test/lib/pynacl/ontap/test/cluster_ha.thpy
In another cluster_ha_original.py script I've this code:
import trace
cmd_to_run = '/x/eng/bbsvl/users/nikhilh/C4563855_4563855_1710110159/test/nate/bin/ntest -noconsole NATE_HOSTS=/u/smoke/presub/tharnhosts/temp_remove/nikhilh NODES=p8020-5 /x/eng/bbsvl/users/nikhilh/C4563855_4563855_1710110159/test/lib/pynacl/ontap/test/cluster_ha.thpy'
tracer = trace.Trace(count=False, trace=True)
tracer.run(cmd_to_run)
r = tracer.results()
r.write_results(summary=True)
The above cluster_ha_original.py file should have given me the complete trace which includes the files, functions and statements which cluster_ha.thpy internally calls but I'm getting a syntax error:
[nikhilh#cycl02 test]$ python3 cluster_ha_original.py
--- modulename: trace, funcname: _unsettrace
trace.py(78): sys.settrace(None)
Traceback (most recent call last):
File "cluster_ha_original.py", line 6, in <module>
tracer.run(cmd_to_run)
File "/usr/software/lib/python3.2/trace.py", line 501, in run
self.runctx(cmd, dict, dict)
File "/usr/software/lib/python3.2/trace.py", line 509, in runctx
exec(cmd, globals, locals)
File "<string>", line 1
/x/eng/bbsvl/users/nikhilh/C4563855_4563855_1710110159/test/nate/bin/ntest -noconsole NATE_HOSTS=/u/smoke/presub/tharnhosts/temp_remove/nikhilh NODES=p8020-5 /x/eng/bbsvl/users/nikhilh/C4563855_4563855_1710110159/test/lib/pynacl/ontap/test/cluster_ha.thpy
^
SyntaxError: invalid syntax
Any ideas?

Related

How to install `distro-info===0.18ubuntu0.18.04.1`?

Trying to modernize an old Django project (2.2), and its requirements.txt (generated via pip freeze) has some lines that make pip install throw fits:
distro-info===0.18ubuntu0.18.04.1
I interpreted the errors I got for the first one (see the error output in its entirety at the bottom) as the version string not conforming to PEP-518, but it doesn't even mention the === operator. This SO thread, What are triple equal signs and ubuntu2 in Python pip freeze?, has a similar issue, but:
The errors they got is different (ValueError as opposed to my ParseError).
The solution was to upgrade pip, but I'm already using the latest one.
Now, pip install distro-info works so should I just go with that?
update: The project I'm trying to update has been conceived around 2020, and according to the PyPI history of distro-info, it had a 0.10 release in 2013 and a 1.0 in 2021. Could this anything have to do with the weird pip freeze output? (From this PyPI support issue.)
The error:
ERROR: Exception:
Traceback (most recent call last):
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3021, in _dep_map
return self.__dep_map
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2815, in __getattr__
raise AttributeError(attr)
AttributeError: _DistInfoDistribution__dep_map
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/packaging/requirements.py", line 102, in __init__
req = REQUIREMENT.parseString(requirement_string)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pyparsing/core.py", line 1141, in parse_string
raise exc.with_traceback(None)
pip._vendor.pyparsing.exceptions.ParseException: Expected string_end, found '(' (at char 12), (line:1, col:13)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3101, in __init__
super(Requirement, self).__init__(requirement_string)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/packaging/requirements.py", line 104, in __init__
raise InvalidRequirement(
pip._vendor.packaging.requirements.InvalidRequirement: Parse error at "'(===0.18'": Expected string_end
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/cli/base_command.py", line 160, in exc_logging_wrapper
status = run_func(*args)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/cli/req_command.py", line 247, in wrapper
return func(self, options, args)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/commands/install.py", line 400, in run
requirement_set = resolver.resolve(
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 92, in resolve
result = self._result = resolver.resolve(
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers.py", line 481, in resolve
state = resolution.resolve(requirements, max_rounds=max_rounds)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers.py", line 373, in resolve
failure_causes = self._attempt_to_pin_criterion(name)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers.py", line 213, in _attempt_to_pin_criterion
criteria = self._get_updated_criteria(candidate)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/resolvelib/resolvers.py", line 203, in _get_updated_criteria
for requirement in self._p.get_dependencies(candidate=candidate):
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/provider.py", line 237, in get_dependencies
return [r for r in candidate.iter_dependencies(with_requires) if r is not None]
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/provider.py", line 237, in <listcomp>
return [r for r in candidate.iter_dependencies(with_requires) if r is not None]
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 247, in iter_dependencies
requires = self.dist.iter_dependencies() if with_requires else ()
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_internal/metadata/pkg_resources.py", line 216, in iter_dependencies
return self._dist.requires(extras)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2736, in requires
dm = self._dep_map
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3023, in _dep_map
self.__dep_map = self._compute_dependencies()
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3033, in _compute_dependencies
reqs.extend(parse_requirements(req))
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3094, in parse_requirements
yield Requirement(line)
File "/home/old-django-project/.venv/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3103, in __init__
raise RequirementParseError(str(e))
pip._vendor.pkg_resources.RequirementParseError: Parse error at "'(===0.18'": Expected string_end
Looks like your library was discontinued. In PyPi, infact, I can see there are only 1.0 and 0.10. If you need that specific version, then you need to setup a manual installation, downloading the source here. Either, you can upgrade your version and try to refactor any possible problem coming after!
In case, if you need to dockerize your app, setting up a script for the manual installation of a library is simple.

I get an Error 6 with Asciimatics and Python

I am learning how to use asciimatics with Python.
When I try to run the following code:
from asciimatics.screen import Screen
from time import sleep
def demo(screen):
screen.print_at('Hello world!', 0, 0)
screen.refresh()
sleep(10)
Screen.wrapper(demo)
I get this error:
Traceback (most recent call last):
File "C:\Users\Patrick\Pictures\Python\Westhope\2.0\test.py", line 20, in <module>
Screen.wrapper(demo)
File "C:\Python27\lib\asciimatics\screen.py", line 1336, in wrapper
unicode_aware=unicode_aware)
File "C:\Python27\lib\asciimatics\screen.py", line 1245, in open
None))
error: (6, 'CreateFile', 'The handle is invalid.')
The short answer is that you are not running in a proper console/terminal window and you need to run inside the standard Windows command prompt instead.
See https://asciimatics.readthedocs.io/en/latest/troubleshooting.html#i-can-t-run-it-inside-pycharm-or-other-ides for more details.

Python can't run ANY script

I have Python 2.7.14 64-bit version installed and I can't run the simplest of scripts. It works normal if I copy to Shell and run from there. This is the script I'm trying to run.
def fun(a):
print a
return
And the error message
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "C:\Python27\lib\idlelib\MultiCall.py", line 166, in handler
r = l[i](event)
File "C:\Python27\lib\idlelib\ScriptBinding.py", line 149, in run_module_event
if PyShell.use_subprocess:
AttributeError: 'module' object has no attribute 'use_subprocess'

How to print the help (usage) section for the main parser of Argparse and not the usage of a subparser?

I have been writing a command line programs with Argparse for some time now, and I am trying to write it in such a way that when the user supplies the following to the command line:
$python my_script.py -h
A help section (usage) will be printed out that prints out help section of the main parser, as well as brief overviews of the subparsers.
But right now, anytime I type in the previous line into my terminal, I receive no usage and instead get a massive traceback and the following error:
TypeError: expected string or buffer
This error has never occurred to me before with argparse-based command line programs. Furthermore, if I supply the name of one of the subparsers,
$python my_script.py subparserA -h
I get a print-out of the subparser's usage. The same holds true for other subparsers.
So why is it not possible for me to get the usage for the main parser? This worked for me before so I don't know why it's not working now. I really would like for the user to be able to look at an overview of the different subparsers available.
My basic code is currently set up in the following way:
import argparse
import sys
if __name__ == "__main__":
Parser = argparse.ArgumentParser(prog= "My_program")
Parser.description= "This program does A and B things."
subparsers= Parser.add_subparsers(help= "SubparserA does A things and SubparserB does B things", dest='mode')
subparserA= subparsers.add_parser("subparserA", help= "Additional explanation of what A things entail")
subparserA.add_arguments("-foo", required=True, help= "foo is needed for SubparserA to work")
subparserB= subparsers.add_parser("subparserB", help="Additional explanation of what B things entail")
subparserB.add_argument("-bar", required=True, help= "bar is needed for SubparserB to work")
args= Parser.parse_args()
if args.mode == "subparserA":
###do things pertinent to subparserA
elif args.mode== "subparserB":
###do things pertinent to subparserB
else:
argparse.print_help()
argparse.ArgumentError("too few arguments")
UPDATE
Here is the full traceback of the error:
Traceback (most recent call last):
File "my_program.py", line 164, in <module>
args= Parser.parse_args()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1701, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1733, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1939, in _parse_known_args
start_index = consume_optional(start_index)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1879, in consume_optional
take_action(action, args, option_string)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1807, in take_action
action(self, namespace, argument_values, option_string)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 996, in __call__
parser.print_help()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2340, in print_help
self._print_message(self.format_help(), file)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 2314, in format_help
return formatter.format_help()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 281, in format_help
help = self._root_section.format_help()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 211, in format_help
func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 485, in _format_text
return self._fill_text(text, text_width, indent) + '\n\n'
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 621, in _fill_text
text = self._whitespace_matcher.sub(' ', text).strip()
TypeError: expected string or buffer
You should be using
Parser.print_help()
Parser.error('too few arguments')
That is use methods of the existing Parser object.
When I run your script
1019:~/mypy$ python stack46754855.py
Traceback (most recent call last):
File "stack46754855.py", line 10, in <module>
subparserA= subparsers.add_parser("subparserA", help= "Additional explanation of what A things entail", dest= 'mode')
File "/usr/lib/python2.7/argparse.py", line 1066, in add_parser
parser = self._parser_class(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'dest'
dest is a not a valid parameter for the add_parser method. It is a valid, and useful, parameter for add_subparsers.
subparsers= Parser.add_subparsers(dest='mode')
It also objects to the add_arguments method.
After correction those I get:
1022:~/mypy$ python stack46754855.py
usage: My_program [-h] {subparserA,subparserB} ...
My_program: error: too few arguments
In Py2, subparsers is a required argument. It is optional in Py3 (a bug), allowing the script to run to the invalid argparse.print_help call:
1022:~/mypy$ python3 stack46754855.py
Traceback (most recent call last):
File "stack46754855.py", line 27, in <module>
argparse.print_help()
AttributeError: module 'argparse' has no attribute 'print_help'
With the change I suggested above:
1025:~/mypy$ python3 stack46754855.py
usage: My_program [-h] {subparserA,subparserB} ...
This program does A and B things.
positional arguments:
{subparserA,subparserB}
SubparserA does A things and SubparserB does B things
subparserA Additional explanation of what A things entail
subparserB Additional explanation of what B things entail
optional arguments:
-h, --help show this help message and exit
usage: My_program [-h] {subparserA,subparserB} ...
My_program: error: too few arguments
The second usage comes from the Parser.error call.
I can't reproduce your
massive traceback and the following error:
TypeError: expected string or buffer
I need to see that traceback (or part of it) to see what exactly is raising the error. That's not a normal argparse error; certainly it isn't one that argparse traps and reroutes.
More on the required/not required subparser behavior at How to Set a Default Subparser using Argparse Module with Python 2.7
Use + instead of , for multi line help string in parser.add_argument. If you have split you argument help in multiple lines using ',' then, you will see this issue
parser.add_argument("xml",help=("long help here",
" long help second line"))
This will result in above exception
instead
parser.add_argument("xml",help=("long help here" +
" long help second line"))

When i tried to rename a txt file in pythonwin

I wrote a script to rename a txt file in pythonwin as below.
import os
os.rename("northrdge.txt","northrdgechg.txt")
the above text file is in the path:C:\Users\gmayil\Desktop\northrdge.txt.
after this when i run the script i met below error:
Can anyone please help me on this ?
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 433, in ImportFile
exec codeObj in __main__.__dict__
File "<auto import>", line 1, in <module>
File "C:\Python27\Tools\gopi\renamefile.py", line 2, in <module>
os.rename("northrdge.txt","northrdgechg.txt")
WindowsError: [Error 2] The system cannot find the file specified
Your python-file lies in "C:\Python27\Tools\gopi\renamefile.py", but you say your txt-file is in "C:\Users\gmayil\Desktop\". If you pass a relative path, python searches in its own directory, which is "C:\Python27\Tools\gopi\".
So all you have to do is giving the complete path:
os.rename("C:\Users\gmayil\Desktop\northrdge.txt", "C:\Users\gmayil\Desktop\northrdgechg.txt")