Reputation: 2873
I'm so sorry about this basic question, because it's similar to this: Stumped by relative imports
But I'm trying to follow the PEP328 http://www.python.org/dev/peps/pep-0328/#guido-s-decision and it doesn't work for me :(
These are my files:
dev@desktop:~/Desktop/test$ ls
controller.py __init__.py test.py
2to3 says all it's right:
dev@desktop:~/Desktop/test$ 2to3 .
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: No files need to be modified.
The content of the files:
dev@desktop:~/Desktop/test$ cat controller.py
class Controller:
def __init__(self):
pass
dev@desktop:~/Desktop/test$ cat __init__.py
# -*- coding: utf-8 -*-
dev@desktop:~/Desktop/test$ cat test.py
#!/usr/bin/env python
from .controller import Controller
if __name__ == '__main__':
print('running...')
But the import it's not working:
dev@desktop:~/Desktop/test$ python3 test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
from .controller import Controller
ValueError: Attempted relative import in non-package
dev@desktop:~/Desktop/test$
Any help is appreciated! Thanks in advance!
Upvotes: 3
Views: 3118
Reputation: 1121864
You cannot use a script within a package; you are running test
, not test.test
. A top-level script can thus not use relative imports.
If you wanted to run a package as a script, you'd need to move test/test.py
to testpackage/__main__.py
, move one directory up in your shell to ~/Desktop
and tell python to run a package with python -m testpackage
.
Demo:
$ ls testpackage/
__init__.py __main__.py __pycache__ controller.py
$ cat testpackage/controller.py
class Controller:
def __init__(self):
pass
$ cat testpackage/__init__.py
# -*- coding: utf-8 -*-
$ cat testpackage/__main__.py
from .controller import Controller
if __name__ == '__main__':
print('running...')
$ python3.3 -m testpackage
running...
You cannot name the package test
; Python already has such a package for the test suite and that'll be found before a package in the current working dir is found.
The alternative is to create a script outside of the package and import the package from the script.
Upvotes: 3