[Tutor] question about sys.path and importing

Dick Moores rdm at rcblue.com
Wed Oct 4 00:02:19 CEST 2006


At 12:30 PM 10/3/2006, you wrote:
>Dick Moores wrote:
>>At 11:38 AM 10/3/2006, Kent Johnson wrote:
>>>Normally you will need to either
>>>- make 'mine' be a package, by creating an empty file named
>>>site-packages\mine\__init__.py, and changing your imports to include the
>>>package name (from mine import mycalc), or
>>>- add site-packages\mine to sys.path, maybe by creating a .pth file.
>>>http://www.python.org/doc/2.4.3/lib/module-site.html
>>I went with your first way, and it works with a script in python25\dev:
>># 1test-8.py
>>from mine import mycalc
>>print mycalc.numberCommas(12341234123)
>>  >>>
>>Evaluating 1test-8.py
>>12,341,234,123
>>But fails here:
>># 1test-9.py
>>from mine import mycalc
>>from mycalc import numberCommas
>>print numberCommas(12341234123)
>>  >>>
>>Evaluating 1test-9.py
>>Traceback (most recent call last):
>>    File "<string>", line 1, in <string>
>>ImportError: No module named mycalc
>>  >>>
>
>Well this is different code.

Yes, that was my point. Sorry I didn't make that clear.

>Try what you did in the first one:
>from mine import mycalc
>print mycalc.numberCommas(12341234123)
>
>or
>from mine.mycalc import numberCommas

Good! Didn't know I could do "from mine.mycalc import numberCommas".

Thanks,

Dick





More information about the Tutor mailing list