PYTHON include_once

is this article helpful?
Python replacement for PHP's include_once [ edit | history ]
import os

def include_once(filename, glob = globals(), modulename = ''):
	if os.path.exists(filename):
		filename = filename[:-3] if filename.endswith('.py') else filename;
		module = modulename if (modulename != '') else filename
		glob[module] =  __import__(filename, globals(), locals(), [], -1)

If you know module name (not in run-time) you can also include once in python with import:
import os
if os.path.exists(''):
	import myfile #Omitting (.py) extension.

Successive call don't have any effect as in include_once php directive as described in python documentation:

For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use reload(), e.g. reload(modulename).

Interactive mode - note:
Import does not enter the names of the object, functions, classes, etc ... defined in directly in the current symbol table. You can anyway access them using module name:
def foo(x,y):
	return x+y*4

a = 3
print 'here we go'

Console - include_once:
>>> import myinclude
>>> myinclude.include_once('')
here we go
>>> myinclude.myfile.a

Console - include_once - with globals():
>>> import myinclude
>>> myinclude.include_once('',globals(),'hello')
here we go
>>> hello.a

Console - python import:
>>> import myfile
here we go
>>> myfile.a

PHP include_once

PHP original manual for include_once [ show | ]


The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

include_once() may be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, so in this case it may help avoid problems such as function redefinitions, variable value reassignments, etc.

See the include() documentation for information about how this function works.

Note: With PHP 4, _once functionality differs with case-insensitive operating systems (like Windows) so for example:

Example #1 include_once() with a case insensitive OS in PHP 4

include_once "a.php"// this will include a.php
include_once "A.php"// this will include a.php again! (PHP 4 only)

This behaviour changed in PHP 5, so for example with Windows the path is normalized first so that C:\PROGRA~1\A.php is realized the same as C:\Program Files\a.php and the file is included just once.