Reputation: 318
root@rebuild:~# python3.4
Python 3.4.0 (default, Nov 27 2014, 13:54:17)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/root', '/root/Python-3.4.0/Lib/site-packages/}', '/usr/local/python3.4/lib/python34.zip', '/usr/local/python3.4/lib/python3.4', '/usr/local/python3.4/lib/python3.4/plat-linux', '/usr/local/python3.4/lib/python3.4/lib-dynload', '/usr/local/python3.4/lib/python3.4/site-packages']
>>> import os
>>> os.system("ls /root/Python-3.4.0/Lib/site-packages/")
test.py README
0
>>> import test.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'test'
It is strange that why can't import test module which is in the sys.path "/root/Python-3.4.0/Lib/site-packages/"?
Upvotes: 0
Views: 1438
Reputation: 5275
import test.py
You imported your module test
with the extension .py
. So the import will try to find module named py
in package test
.
import test
Would be the correct syntax for importing a module.
For example :
>>> import string
>>> string
<module 'string' from 'C:\Python27\lib\string.pyc'>
Here string
is module. But if you try to import string.py
it treats string
as package
and trys to import
py
module.
>>> import string.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py
And you should also fix your site-packages
path in sys.path
which has '}' symbol at the end which is invalid path.
Upvotes: 1
Reputation: 8910
For two reasons:
sys.path
is messed up (there's a trailing }
).import test
, not import test.py
.Please read the Python tutorial's section on modules: https://docs.python.org/3/tutorial/modules.html
Upvotes: 0