[Windows 10 / Python 3.9 / Pipenv]How to debug an error when using `pipenv install xxx` from command line

Viewed 127

I'm having trouble installing things without error when using Pipenv and am unable to figure out how to fix the error. Just for some upfront clarification, I'm using Windows 10, Python 3.9 and the issue is when I install with Pipenv. My last name is a little odd and has an apostrophe in it; however, I usually leave the apostrophe out when registering for things due to potential issues with the apostrophe. For some reason, at work, the IT department created my account using the apostrophe. Since it is configured with Azure AD, I cannot adjust the username from my own PC, even though I have admin rights on this machine.

The error seems to be coming from my opening a JSON file in my appdata directory, which , when fully qualified contains the string "t'alua" (my last name). Unfortunately, the string seems to be enclosed in single quotes so the apostrophe in my name causes a parsing issue. Just for the record, I have even uninstalled python from my C drive and installed on my D drive and executed commands in my D drive (where there is no apostrophe in the working directory's path) but I still get the same issue since it is related to the appdata directory's absolute path. I have also asked the IT department to change my username to remove the apostrophe but they said its not feasible due to so many downstream changes that would be required to prevent any kind of bugs, which I understand.

Here is an example command with the error shown:

(corl_anl_coercion-sJyNKu6O) D:\mttf_mtbf_datasets\training_data\corl_anl_coercion>pipenv install numpy

Installing numpy...
[=   ] Installing numpy...Failed to load paths:   File "<string>", line 1
    import sysconfig, distutils.sysconfig, io, json, sys; paths = {u'purelib': u'{0}'.format(distutils.sysconfig.get_python_lib(plat_specific=0)),u'stdlib': u'{0}'.format(sysconfig.get_path('stdlib')),u'platlib': u'{0}'.format(distutils.sysconfig.get_python_lib(plat_specific=1)),u'platstdlib': u'{0}'.format(sysconfig.get_path('platstdlib')),u'include': u'{0}'.format(distutils.sysconfig.get_python_inc(plat_specific=0)),u'platinclude': u'{0}'.format(distutils.sysconfig.get_python_inc(plat_specific=1)),u'scripts': u'{0}'.format(sysconfig.get_path('scripts')),u'py_version_short': u'{0}'.format(distutils.sysconfig.get_python_version()), }; value = u'{0}'.format(json.dumps(paths));fh = io.open('c:/users/t'alua~1/appdata/local/temp/tmp0m5ovbrb.json', 'w'); fh.write(value); fh.close()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ^
SyntaxError: invalid syntax

Output:
[  ==] Installing numpy...Failed to load paths:   File "<string>", line 1
    import sysconfig, distutils.sysconfig, io, json, sys; paths = {u'purelib': u'{0}'.format(distutils.sysconfig.get_python_lib(plat_specific=0)),u'stdlib': u'{0}'.format(sysconfig.get_path('stdlib')),u'platlib': u'{0}'.format(distutils.sysconfig.get_python_lib(plat_specific=1)),u'platstdlib': u'{0}'.format(sysconfig.get_path('platstdlib')),u'include': u'{0}'.format(distutils.sysconfig.get_python_inc(plat_specific=0)),u'platinclude': u'{0}'.format(distutils.sysconfig.get_python_inc(plat_specific=1)),u'scripts': u'{0}'.format(sysconfig.get_path('scripts')),u'py_version_short': u'{0}'.format(distutils.sysconfig.get_python_version()), }; value = u'{0}'.format(json.dumps(paths));fh = io.open('c:/users/t'alua~1/appdata/local/temp/tmpywegc7gn.json', 'w'); fh.write(value); fh.close()
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ^
SyntaxError: invalid syntax

Output:
Adding numpy to Pipfile's [packages]...
Installation Succeeded
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...

Resolving dependencies...
Locking Failed!
Traceback (most recent call last):
  File "D:\installations\python\lib\site-packages\pipenv\resolver.py", line 764, in <module>
    main()
  File "D:\installations\python\lib\site-packages\pipenv\resolver.py", line 758, in main
    _main(parsed.pre, parsed.clear, parsed.verbose, parsed.system, parsed.write,
  File "D:\installations\python\lib\site-packages\pipenv\resolver.py", line 741, in _main
    resolve_packages(pre, clear, verbose, system, write, requirements_dir, packages, dev)
  File "D:\installations\python\lib\site-packages\pipenv\resolver.py", line 702, in resolve_packages
    results, resolver = resolve(
  File "D:\installations\python\lib\site-packages\pipenv\resolver.py", line 684, in resolve
    return resolve_deps(
  File "d:\installations\python\lib\site-packages\pipenv\utils.py", line 1406, in resolve_deps
    results, hashes, markers_lookup, resolver, skipped = actually_resolve_deps(
  File "d:\installations\python\lib\site-packages\pipenv\utils.py", line 1119, in actually_resolve_deps
    resolver.resolve()
  File "d:\installations\python\lib\site-packages\pipenv\utils.py", line 834, in resolve
    results = self.resolver.resolve(max_rounds=environments.PIPENV_MAX_ROUNDS)
  File "d:\installations\python\lib\site-packages\pipenv\utils.py", line 822, in resolver
    self.get_resolver(clear=self.clear, pre=self.pre)
  File "d:\installations\python\lib\site-packages\pipenv\utils.py", line 813, in get_resolver
    constraints=self.parsed_constraints, repository=self.repository,
  File "d:\installations\python\lib\site-packages\pipenv\utils.py", line 806, in parsed_constraints
    self._parsed_constraints = [c for c in self.constraints]
  File "d:\installations\python\lib\site-packages\pipenv\utils.py", line 806, in <listcomp>
    self._parsed_constraints = [c for c in self.constraints]
  File "d:\installations\python\lib\site-packages\pipenv\patched\notpip\_internal\req\req_file.py", line 137, in parse_requirements
    for parsed_line in parser.parse(filename, constraint):
  File "d:\installations\python\lib\site-packages\pipenv\patched\notpip\_internal\req\req_file.py", line 282, in parse
    for line in self._parse_and_recurse(filename, constraint):
  File "d:\installations\python\lib\site-packages\pipenv\patched\notpip\_internal\req\req_file.py", line 287, in _parse_and_recurse
    for line in self._parse_file(filename, constraint):
  File "d:\installations\python\lib\site-packages\pipenv\patched\notpip\_internal\req\req_file.py", line 329, in _parse_file
    args_str, opts = self._line_parser(line)
  File "d:\installations\python\lib\site-packages\pipenv\patched\notpip\_internal\req\req_file.py", line 365, in parse_line
    shlex.split(options_str), defaults)  # type: ignore
  File "D:\installations\python\lib\shlex.py", line 315, in split
    return list(lex)
  File "D:\installations\python\lib\shlex.py", line 300, in __next__
    token = self.get_token()
  File "D:\installations\python\lib\shlex.py", line 109, in get_token
    raw = self.read_token()
  File "D:\installations\python\lib\shlex.py", line 191, in read_token
    raise ValueError("No closing quotation")
ValueError: No closing quotation

I can see from the output above that the error is due to this line of python being executed:

fh = io.open('c:/users/t'alua~1/appdata/local/temp/tmp0m5ovbrb.json', 'w');

since the string is not terminated correctly.

Unfortunately, I'm not sure where this is coming from, though. I tried to read through each of the python files listed in the stack trace and I see where, in shlex.py if the string ends, while still being in a 'quoted state', then it raises the "No closing Quotation" valueerror. I couldn't see any line of code where it actually pulls the appdata path or why it is using appdata in the first place. I did find in my python installation's site packages directory's util.py:

D:\installations\python\Lib\site-packages\pipenv\utils.py

that there is an escaped_grouped_arguments function. I thought perhaps if the filepath is being parsed with this, I can modify the function to check for variations of my lastname and replace it with a good value so I made the following change:

def escape_grouped_arguments(s): """Prepares a string for the shell (on Windows too!)

  Only for use on grouped arguments (passed as a string to Popen)
  """
  if s is None:
      return None
  ## This is the original code
  # Additional escaping for windows paths
  #if os.name == "nt":
  #    s = "{}".format(s.replace("\\", "\\\\"))
  #return '"' + s.replace("'", "'\\''") + '"'
  ## This is my modification
  if "t'alua" in s.lower():
      bad_names = ["T'Alua","t'alua","T'alua","T'ALUA"]
      good_name = "talua"
      for bad_name in bad_names:
          s = s.replace(bad_name, good_name)
  if os.name == "nt":
      s = "{}".format(s.replace("\\", "\\\\"))
  return '"' + s.replace("'", "'\\''") + '"'

But this change didn't seem to resolve the errors. Any and all guidance on how to correctly spot exactly where the error is coming from as well as how to best fix this particular issue would be greatly appreciated.

0 Answers
Related