繁体   English   中英

从PyCharm运行python脚本。 导入皮查姆

[英]Run python script from PyCharm. Import in Pycharm

好像Pycharm 1.5.3产生了一些魔力,或者我错过了一些东西。

我有以下文件结构:

/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时,这是在控制台中编写的:

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运行此脚本时-一切正常。 我收到“导入错误:没有名为工具的模块”

您提供的示例中的导入工作是因为python路径包含D:\\\\Workspace\\\\Python\\\\sp ,这是您的工具模块所在的位置。

当您从标准python解释器运行模块时,它仅将路径添加到sys.path当前模块。

您在/sp目录中缺少__init__.py 创建具有该名称的文件,您应该可以导入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM