ImportError depending on the calling module

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Sep 6 16:01:11 EDT 2007


En Thu, 06 Sep 2007 14:41:42 -0300, Samuel <knipknap at gmail.com> escribi�:
> On Sep 6, 6:44 pm, Gabriel Genellina <gagsl-... at yahoo.com.ar> wrote:
>> On 6 sep, 13:13, Samuel <knipk... at gmail.com> wrote:
>>
>> That's fine, and a good requirement. Place start2.py inside the
>> container directory, and your package beneath it. Then you don't have
>> to modify sys.path to find the package - "import packagename" just
>> works. And it still works if the user installs the package (into site-
>> packages, by example).
>>
>>  start1.py
>>  start2.py
>>  packagename/
>>  |-- Obj.py
>>  |-- __init__.py
>>  |-- foo/
>>      |-- FooTest.py
>>      `-- __init__.py
>>
>> Packages are... well, packages, boxes. Don't try to import a module
>> inside a package as it were a standalone module
>
> But the start2.py script *is* part of the library. It should be in the
> package.

If it imports the package, it cannot be part of the package itself.

> However, I just found that Python 2.5 introduces relative imports.
> This sounds like an attempt to remedy the situation.

For relative imports to work, the importing module must know that it is  
part of a package. That is not the case if you run a script located inside  
the package.

-- 
Gabriel Genellina




More information about the Python-list mailing list