chrisxfire
chrisxfire

Reputation: 635

ModuleNotFound: dateutil.parser

Here's what I'm running into:

(.venv) PS C:\Users\<redacted>\onedrive\dev\python\code\kb4> py kb4.py
Traceback (most recent call last):
  File "C:\Users\hanawayc\onedrive\dev\python\code\kb4\kb4.py", line 7, in <module>
    from dateutil.parser import parse
ModuleNotFoundError: No module named 'dateutil'

However:

(.venv) PS C:\Users\<redacted>\onedrive\dev\python\code\kb4> py
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from dateutil.parser import parse
>>> quit()

The import statements in kb4.py are as follows:

from dateutil.parser import parse
from os import getcwd, mkdir
from os.path import exists
from sys import exit, stdout
from time import sleep, strftime
import json
import logging
import pandas as pd
import requests

If it matters, here is my pip list:

Package            Version
------------------ ---------
astroid            2.6.5
backcall           0.2.0
certifi            2021.5.30
charset-normalizer 2.0.3
colorama           0.4.4
debugpy            1.4.1
decorator          5.0.9
idna               3.2
ipykernel          6.0.3
ipython            7.25.0
jedi               0.18.0
jupyter-client     6.1.12
jupyter-core       4.7.1
lazy-object-proxy  1.6.0
numpy              1.21.1
pandas             1.3.1
parso              0.8.2
pickleshare        0.7.5
prompt-toolkit     3.0.19
Pygments           2.9.0
pylint             2.9.6
python-dateutil    2.8.2
pywin32            301
pyzmq              22.1.0
requests           2.26.0
setuptools         56.0.0
six                1.16.0
toml               0.10.2
tornado            6.1
traitlets          5.0.5
urllib3            1.26.6
wcwidth            0.2.5
wrapt              1.12.1

I have tried:

Any ideas on what else I might try?

Upvotes: 2

Views: 229

Answers (1)

Alejandro QA
Alejandro QA

Reputation: 374

Your question is indeed interesting! I could reproduce your scenario and obtain the same output, using the pandas library for testing:

(myvenv) RANGO@RANGO-PC D:\myvenv
> py testpandas.py
Traceback (most recent call last):
  File "D:\myvenv\testpandas.py", line 3, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'

(myvenv) RANGO@RANGO-PC D:\myvenv
> py
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>>

You are using py to launch Python, so visiting the Python Launcher's documentation I found a clue of what may be happening in your situation.

You say that there are 6 lines of comments before the import that fails. Maybe one of those comments is a shebang line which redirects to another Python installation on your environment.

My test script is the following:

#! C:\msys64\mingw64\bin\python
#
import pandas as pd

s = pd.Series([1, 3, 5, 12, 6, 8])
print(s)

There you have one shebang line and a comment line before the import line. The shebang line is forwarding the execution and environment to another python installation different than the "default" one, as the following test shows.

(myvenv) RANGO@RANGO-PC D:\myvenv
> C:\msys64\mingw64\bin\python -V
Python 3.9.7

(myvenv) RANGO@RANGO-PC D:\myvenv
> py -V
Python 3.9.0

Upvotes: 1

Related Questions