Question

It seems like Pycharm 1.5.3 makes some magic, or I missed something.

I have following file structure:

/sp
/sp/tools.py
/sp/test/main.py

Files contain following code

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)

When I run main.py this is written in console:

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

I don't understand why import function knows about this module 'tools': . When I run this script from windows cmd - everything is okay. I get 'ImportError: No module named tools'

Was it helpful?

Solution

The import work in the example you provided because your python path contains D:\\Workspace\\Python\\sp which is where your tools module is.

When you run a module from standard python interpreter, it only adds the path to current module in sys.path.

OTHER TIPS

You're missing an __init__.py in the /sp directory. Create a file with that name, and you should be able to import.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top