-2

Is it possible to avoid importing a file with from file import function? Someone told me i would need to put an underscore as prefix, like: _function, but isn't working.

I'm using Python 2.6 because of a legacy code.

  • That's not possible in Python. You can influence what is imported on `from .. import *` though, using `__all__` (and leading underscores). [Name mangling](http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python) is related as well. – Phillip Jan 18 '17 at 21:17
  • You also don't want to think about it as "importing a file". You're importing a function from a module. – Jon Kiparsky Jan 18 '17 at 21:21
  • I don't know that this question needs to be voted down - the question is valid. The best answers should include some explanation of Python conventions to help others who may have this question in the future. Perhaps explaining a bit more about what you mean by "isn't working" and why you want to do this would help? – clyde Jan 18 '17 at 21:30

2 Answers2

3

There are ways you can prevent the import, but they're generally hacks and you want to avoid them. The normal method is to just use the underscore:

def _function():
    pass

Then, when you import,

from my_module import *

You'll notice that _function is not imported because it begins with an underscore. However, you can always do this:

# In Python culture, this is considered rude
from my_module import _function

But you're not supposed to do that. Just don't do that, and you'll be fine. Python's attitude is that we're all adults. There are a lot of other things you're not supposed to do which are far worse, like

import my_module
# Remove the definition for _function!
del my_module._function
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Thanks @DietrichEpp. Could you give me your opinion on this question: http://stackoverflow.com/questions/41727874/proper-way-of-implementing-one-function-per-file-in-python/41727967#41727967 ? I would love to read your opinion. –  Jan 18 '17 at 21:27
2

There is no privacy in Python. There are only conventions governing what external code should consider publicly accessible and usable.

Importing a module for the first time, triggers the creation of a module object and the execution of all top-level code in the module. The module object contains the global namespace with the result of that code having run.

Because Python is dynamic you can always introspect the module namespace; you can see all names defined, all objects those names reference, and you can access and alter everything. It doesn't matter here if those names start with underscores or not.

So the only reason you use a leading _ underscore for a name, is to document that the name is internal to the implementation of the module, and that external code should not rely on that name existing in a future version. The from module import * syntax will ignore such names for that reason alone. But you can't prevent a determined programmer from accessing such a name anyway. They simply do so at their own risk, it is not your responsibility to keep them from that access.

If you have functions or other objects that are only needed to initialise the module, you are of course free to delete those names at the end.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343