import - Python includes, module scope issue -


I am working on my first important Python project and I am having trouble connecting with scope issues and files. . Previously my experience was with PHP.

What I would like to do is the same file that sets up several configuration variables, which will then be used throughout the code. Also, I want to create some functions and classes available globally. For example, the main file will contain another file, and that file will load normally a function (in each file) and a bunch of configuration files. Within those loaded files, I also want to be able to use function and configuration variables. To include everything else, I have to put a full routine at the beginning of every (included) file, and these files are included in various sub-directories, which are very difficult to import them. (Especially if I have to import it again in every file).

Anyway I'm normal

Thanks!

itemprop = "text">

In Python, it is a general practice that a bunch of modules implementing different functions And then there is a single module which has point-to-access for all functions. It's basically it's.

An example: You are writing a package foo , which contains bar , baz , and moo module.

  ~ / project / foo ~ / project / foo / __ init__.py ~ / project / foo / bar.py ~ / project / foo / biz.py ~ / project / foo / moo .py ~ / project / foo / config.py  

What will you usually do __init __? Py In this way: Import

  import from foo.bar from func1, foo.baz import func3 from func2, foo.moo from constant1, import from func1 to foo as moofunc1. Config import *  

Now, when you want to use functions, you only

  import foo foo.func1 () print foo.constant1 # Configure config1 variable defines the print foo.config1  

If you want, you can organize your code so that you can only

  Import foo  

at the top of each module, and then foo (which you probably have to say about the NME "Globals" or something for that effect) if you do not like the namespace, then you import

  and can do everything globally, but it is not really recommended. Remember: Nameshoot is a great idea! 


Comments