Sa Oh
Sa Oh

Reputation: 145

Why I get "ImportError" only when I run a python script?

I have a Python script where I import datadog module. When I run python datadog.py, it fails with ImportError: cannot import name statsd. The script starts with following lines:

import os
import mysql.connector

from time import time
from datadog import statsd

Actual error messages are following:

$ python /mnt/datadog.py
Traceback (most recent call last):
  File "/mnt/datadog.py", line 5, in <module>
    from datadog import statsd
  File "/mnt/datadog.py", line 5, in <module>
    from datadog import statsd
ImportError: cannot import name statsd

But when I'm in Python shell (started by python command), I can successfully run from datadog import statsd. What's the difference here?

By the way, I have proper Python packages installed in my computer:

$ pip freeze | egrep 'datadog|mysql'
datadog==0.17.0
mysql-connector==2.1.6

$ python --version
Python 2.7.5

Upvotes: 1

Views: 652

Answers (2)

Arjun P P
Arjun P P

Reputation: 39

First install statsd by

pip install statsd

then do

import statsd

Upvotes: -1

user803422
user803422

Reputation: 2814

The problem is that your script is named datadog.py. So when it imports the module datadog, it imports itself.

Upvotes: 4

Related Questions