0

Hello I am trying to execute a python script in the Ubuntu subsystem I enabled in Windows.

The reason I want to do this is because I have scheduled with crontab to run this script daily.

The problem I have is that the same script that passes when I run on normal Windows terminal fails on the ubuntu terminal. I am executing with Pyhon3 in both cases.

The problem seems to be related to how I am processing the json data.

What the code does is to get the response an API call returns, add add its contents in an excel file with the help of the tablib library which I have installed.

call_api_call method returns the json in this form: return json.loads(response.text)

Related Part of Code:

dataset_list = call_api_call() 

data = tablib.Dataset()

data.json = json.dumps(dataset_list)
data_export = data.export('xlsx')

filename = os.path.dirname(os.path.realpath(__file__)) + '_' + str(datetime.date.today()) + '.xlsx'

with open(filename, 'wb') as f:  
  f.write(data_export)  # write issues to the xlsx file

Running from Windows on an Ubuntu terminal:

Execute with:

python3  /mnt/c/Users/marialena/source/repos/GitLab_Issues/gitlab.py -vs

Output:

Traceback (most recent call last):
  File "/mnt/c/Users/marialena/source/repos/GitLab_Issues/gitlab.py", line 93, in <module>
    data1 = json.loads(list_with_bugs)
  File "/usr/lib/python3.6/json/__init__.py", line 348, in loads
    'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

Running from Windows normal terminal:

Execute with:

PS C:\Users\marialena\source\repos\GitLab_Issues> python .\gitlab.py

Output: No error, File gets created with no issue.


Update: I have updated python from 3.6.8 to 3.7.3 in the unix subsystem using the following command to match the windows version (after i have installed again tablib with python3.7 -m pip install tablib)

python3.7  /mnt/c/Users/marialena/source/repos/GitLab_Issues/gitlab.py -vs

When I execute now the script I get

   Traceback (most recent call last):
  File "/mnt/c/Users/marialena/source/repos/GitLab_Issues/gitlab.py", line 93, in <module>
    data.json = json.dumps(list_with_bugs)
  File "/usr/local/lib/python3.7/dist-packages/tablib/formats/_json.py", line 39, in import_set
    dset.dict = json.loads(in_stream)
  File "/usr/local/lib/python3.7/dist-packages/tablib/core.py", line 381, in _set_dict
    if isinstance(pickle[0], list):
KeyError: 0
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
    import apport.fileutils
  File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
    import apt
  File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
Traceback (most recent call last):
  File "/mnt/c/Users/marialena/source/repos/GitLab_Issues/gitlab.py", line 93, in <module>
    data.json = json.dumps(list_with_bugs)
  File "/usr/local/lib/python3.7/dist-packages/tablib/formats/_json.py", line 39, in import_set
    dset.dict = json.loads(in_stream)
  File "/usr/local/lib/python3.7/dist-packages/tablib/core.py", line 381, in _set_dict
    if isinstance(pickle[0], list):
KeyError: 0

1 Answer 1

1

There is a ModuleNotFoundError at the traceback for apt_pkg.

You could try reinstalling that package, and checking if it works.

sudo apt-get remove --purge python-apt
sudo apt-get install python-apt
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this now.. I get exactly the same error. Not sure if the python versions are confused somehow in the subsystem.
Try using which python (On UNIX subsystem) and where python (On Windows). It will give you where the python command is getting the executable to run. It uses PATH environment variable, so if you find any issues, you can try editting the PATH.
In unix I get /usr/bin/python and windows C:\Users\marialena\AppData\Local\Programs\Python\Python37\python.exe C:\Users\marialena\AppData\Local\Microsoft\WindowsApps\python.exe... In my PATH environmental variable I have %USERPROFILE%\AppData\Local\Programs\Python\Python37 which is reachable. Is there a problem with the executables? Both unix and windows seem to find the correct paths to execute the script using python3.
You can test this by directly calling the executable with full path
I uninstalled and installed again python3-apt and now this seems to work.. I did sudo apt-get remove python3-apt and sudo apt-get install python3-apt . So it was like what you suggested, I just missed that it was python and not python3.. I still get an issue but this is unrelated to the unix subsystem. Closing this bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.