Pycharm 1.5.3似乎有些神奇,或者我錯過了一些東西。從PyCharm運行python腳本。在Pycharm中導入
我有以下文件結構:
/sp
/sp/tools.py
/sp/test/main.py
文件包含以下代碼
main.py:
__author__ = 'username'
import tools
import sys
def test1():
print locals()
print globals()
print sys.path
print test1
if __name__ == '__main__':
test1()
tools.py:
class SettingsDictionary(dict):
def __init__(self, seq, **kwargs):
dict.__init__(self, seq, **kwargs)
當我運行main.py這是w雷農控制檯:
C:\Python27\python.exe D:/Workspace/Python/sp/test/main.py
{}
{'test1': <function test1 at 0x0143D0B0>, '__warningregistry__': {("Not importing directory 'C:\\Program Files\\JetBrains\\PyCharm 1.5.3\\helpers\\tools': missing __init__.py", <type 'exceptions.ImportWarning'>, 2): True}, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'D:/Workspace/Python/sp/test/main.py', '__author__': 'p.grechishkin', 'sys': <module 'sys' (built-in)>, '__name__': '__main__', '__package__': None, 'tools': <module 'tools' from 'D:\Workspace\Python\sp\tools.pyc'>, '__doc__': None}
['D:\\Workspace\\Python\\sp\\test', 'C:\\Python27\\lib\\site-packages\\simplejson-2.1.6-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\django_staticfiles-1.1.2-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\django_appconf-0.4-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\suds-0.4-py2.7.egg', 'C:\\Program Files\\JetBrains\\PyCharm 1.5.3\\helpers', 'D:\\Workspace\\Python\\sp', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin']
<function test1 at 0x0143D0B0>
Process finished with exit code 0
我不明白爲什麼導入功能知道這個模塊的「工具」。 當我從Windows cmd運行這個腳本 - 一切都很好。我得到'ImportError:No module named tools'
是否有一些PyCharm函數將每個目錄從'tool.py'路徑添加到sys.path中?看起來像魔術。 – sunprophit
我不知道,我不使用該工具。你說在添加__init__.py之後它可以在windows cmd中工作。 pycharm下的sys.path現在還是一樣嗎? –
是的,添加__init__.py後是一樣的。就像在添加__init__.py之前一樣。 – sunprophit