[Tutor] Import vs #include

Warren Marshall epicboy at gmail.com
Fri Sep 18 22:15:11 CEST 2009


Excellent, OK, this is becoming clearer ...

So if I wanted a common library of code that several Python apps would  
be using, best practices would say I should put that into a directory  
that the projects can see and import it as a package.module.  Cool...

- Warren
(epicboy at gmail.com)



On Sep 18, 2009, at 3:25 PM, Kent Johnson wrote:

> On Fri, Sep 18, 2009 at 2:14 PM, Warren Marshall <epicboy at gmail.com>  
> wrote:
>>
>> I'm trying to get my head around the organization of a larger Python
>> project.
>>
>> 1. Am I right in thinking that in Python, you don't have the  
>> concept of
>> something like a precompiled header and that every file that wants  
>> to use,
>> say "vector.py" needs to import that module?
>
> Yes.
>
>> 2. How are Python projects typically organized  in terms of having  
>> many
>> files.  Are sub-directories for different kinds of files (rendering  
>> files go
>> here, file management files go here, etc), or does that not play  
>> nicely with
>> the import command?
>
> It's fine. The directories are called packages and must contain a
> (possibly empty) file named __init__.py. Then you can do things like
>  from package.module import SomeClass
>
> See the std lib for examples, for example the email and logging  
> packages.
>
>> 3. As you can tell, I've done a lot of C/C++/C# and I'm trying to  
>> shake
>> loose the analog that I've built up in my head that import is  
>> Python's
>> answer to #include.  It isn't, is it?
>
> Not really, it is more like a using declaration in C# except it
> doesn't bring the contents of the module into scope, just the module
> itself.
>  using System; // C#
> is like
>  from sys import * # Python
>
> though the latter form is discouraged in favor of just
>  import sys
> or importing the specific items you need:
>  from sys import modules
>
> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list