What does ".main" mean in python source code? - python

In a source code in python: usr/local/lib/python3.3/unittest/__init__.py
from .result import TestResult
from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
skipUnless, expectedFailure)
from .suite import BaseTestSuite, TestSuite
from .loader import (TestLoader, defaultTestLoader, makeSuite, getTestCaseNames,
findTestCases)
from .main import TestProgram, main
from .runner import TextTestRunner, TextTestResult
from .signals import installHandler, registerResult, removeResult, removeHandler
I can't understand .result and .main. Why do they have a dot prefix in the name?

It's called a relative import.
It means you import from the module in the same directory that the module this code is in. Without the dot, it would import the from first module found in the PYTHON PATH.

You are importing main module which is in the same package that your file, you are doing a relative import (dot prefix). More on relative imports on PEP 328

Related

What I'm doing wrong while using __init__.py?

I have the following project structure:
stages
--Transform
----__init__.py
----Transformer.py
----PDFTransformer.py
core.py
I would like to import PDFTransformer like this:
from stages.Transform import PDFTransformer
Therefore I have created stages/Transform/__init__.py with the following content in it:
from .Transformer import Transformer
from .PDFTransformer import PDFTransformer
But python interpreter throws the following error (in PDFTransformer.py):
ModuleNotFoundError: No module named 'Transformer'
The PDFTransformer.py includes the following:
import Transformer
class PDFTransformer (Transformer):
pass
What I`m doing wrong?
If you ask Python to import Transformer from PDFTransformer.py, but when PDFTransformer.py is imported from core.py - i.e. when core.py is the __main__ file - then the import Transformer is relative to core.py
In other words it is as if you wrote import Transformer in core.py. That obviously wouldn't work.
Instead in PDFTransformer.py put from . import Transformer, and it will import Transformer.py
add __all__ =['Transformer', 'PDFTransformer'] in stages/Transform/__init__.py file.
so this __init__.py file would be like
from .Transformer import Transformer
from .PDFTransformer import PDFTransformer
__all__ =['Transformer', 'PDFTransformer']
this tell the python to load these function when you do from stages.Transform import PDFTransformer

Python: A problem with the package import from ... import * using __all__ and __init__

I have the following Python package with 2 moludes:
-pack1
|-__init__
|-mod1.py
|-mod2.py
-import_test.py
with the code:
# in mod1.py
a = 1
and
# in mod2.py
from mod1 import a
b = 2
and the __init__ code:
# in __init__.py
__all__ = ['mod1', 'mod2']
Next, I am trying to import the package:
# in import_test.py
from pack1 import *
But I get an error:
ModuleNotFoundError: No module named 'mod1'
If I remove the dependency "from mod1 import a" in mod2.py, the import goes correctly. But that dependency makes the import incorrect with that exception "ModuleNotFoundError".
???
The issue here is that from mod2 perspective the first level in which it will search for a module is in the path from which you are importing it (here I am assuming that pack1 is not in your PYTHONPATH and that you are importing it from the same directory where pack1 is contained).
This means that if pack1 is in the directory /dir/to/pack1 and you do:
from mod1 import a
Python will look for mod1 in the same directory as pack1, i.e., /dir/to/pack1.
To solve your issue it is enough to do either:
from pack1.mod1 import a
or in Python 3.5+
from .mod1 import a
As a side note, unless this is a must for you, I do not recommend designing your package to be used as from pack import *, even if __all__ exists to give you better control of your public API.

Python Paths - ImportError: cannot import name 'dataset_builder'

I am getting an error ImportError: cannot import name 'dataset_builder'. The import command is following:
from object_detection.builders import dataset_builder
The file tree looks following:
-ObjectDetection
-train.py
-models
-research
-object_detection
-builders
- __init__.py
-dataset_builder.py
I am running the train.py from the root directory (os.getcwd() returns following path C:\Users\horakm\PyCharmProjects\ObjectDetection) and I added in the train.py following code to add paths:
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research\slim')
When I print all paths using sys.path i get this:
['C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\python36.zip',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\DLLs',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\object_detection-0.1-py3.6.egg',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research\\slim']
How is it possible that the import statement doesn't work?
Which object are you actually trying to import?
All that are defined in dataset_builder? In that case it should be from object_detection.builders.dataset_builder import *.
Or is the object also called dataset_builder? In that case it is from object_detection.builders.dataset_builder import dataset_builder.

Python : Cannot find any functions within my own module?

I have created my own module with filename mymodule.py. The file contains:
def testmod():
print "test module success"
I have placed this file within /Library/Python/2.7/site-packages/mymodule/mymodule.py
I have also added a __init__.py file, these have compiled to generate
__init__.pyc and mymodule.pyc
Then in the python console I import it
import mymodule
which works fine
when I try to use mymodule.testmod() I get the following error:
AttributeError: 'module' object has no attribute 'testmod'
So yeah it seems like it has no functions?
You have a package mymodule, containing a module mymodule. The function is part of the module, not the package.
Import the module:
import mymodule.mymodule
and reference the function on that:
mymodule.mymodule.testmod()
You can use from ... import and import ... as to influence what gets imported exactly:
from mymodule import mymodule
mymodule.testmod()
or
from mymodule import mymodule as nestedmodule
nestedmodule.testmod
or
from mymodule.mymodule import testmod
testmod()
etc.

Import a module from a package

I'm trying to import a simple package, but it doesn't work.
I have a "package" directory that contains two files:
foo.py (with a function called fct)
__init__.py (with nothing in it)
Here is the content of tests.py:
import package.foo
foo.fct(7)
But it doesn't work.
If I change the import line to from package.foo import fct, I can execute the function.
You need import package.foo as foo or one of the alternatives below ...
import package.foo
package.foo.fct(7)
or:
import package.foo as foo
foo.fct(7)
or possibly:
from package import foo
foo.fct(7)

Categories