Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why running a same script in different location cause errors?

Tags:

python

I try to run a script that's using third party module (completely install by pip install module) in Ubuntu 13.10. This script located in NTFS partition.

import foo

API_KEY = "xxx"
api = foo.FOO(APIKEY)

and it's return error like

$ python test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import foo
  File "/media/path/to/project/test.py", line 6, in <module>
    api = foo.Foo(API_KEY)
AttributeError: 'module' object has no attribute 'Foo'

but when I copy a same code to ~/Desktop/test.py then it's work correctly. Is script's location cause these error or if it's not what's the problem?


1 Answers

Check whether there is foo.py file in the directory. If there's one, it could prevent importing the module you want.

Issuing following command in both directory will give you the path of foo module:

python -c "import foo; print(foo.__file__)"

If you find there's unexpected foo.py, rename it or remove it. (Make sure there's no foo.pyc remain)

like image 159
falsetru Avatar answered Jan 02 '26 16:01

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!