ImportError: cannot import name 'datetime'


#1

Hi All, I’m new to both Atom and Python. Using Atom to practise Python. My problem is that I can’t successfully import python modules.

I wrote a short piece of code to conflate the contents of three .txt files into one new file (with the new filename being the date/time of when the file was created. I was advised that rather than create a python list for the original 3 .txt files, I could use the glob module to iterate over the file path and pick out the files based on extension. The code looks like this:

import glob2
from datetime import datetime

filenames = glob2.glob("*.txt")
with open(datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f")+".txt", ‘w’) as file:
for filename in filenames:
with open(filename, “r”) as f:
file.write(f.read() + “\n”)

The problem is that when I execute it from here:

C:\users\rball\OneDrive - Personal\AtomForPython> py concatglob.py

I get this error:

Traceback (most recent call last):

File “concatglob.py”, line 1, in
import glob2
ModuleNotFoundError: No module named ‘glob2’

As you can see, Atom is tied to my OneDrive account which is where my files are kept.

Python is installed on C:\ but Atom can definitely see the module paths as shown below:

C:\users\rball\OneDrive - Personal\AtomForPython> py -c “import sys;print(sys.path);”

[’’, ‘C:\Program Files\Python36\python36.zip’, ‘C:\Program Files\Python36\DLLs’, ‘C:\Program Files\Python36\lib’, ‘C:\Program Files\Python36’, ‘C:\Program Files\Python36\lib\site-packages’]

Any ideas?


#2

Check within the Python script that it’s running the right version.


#3

Solved!

I had a practise file which I had named ‘datetime.py’. This was causing issues when I attempted to import the datetime module.

Deleting the file solved the issue.


#4

Hi Aerijo,

Thank you for taking the time to respond. Much appreciated.