Importing Classes & Modules with __init__.py
up vote
0
down vote
favorite
During development, I want to import a class from file_A.py
into file_B.py
, without using the sys.path
method described here. For reference, my system specs and file tree are below.
OS: Ubuntu 18.04
Python: 3.6.4
parent_folder/
├── project_A
│ ├── file_A.py
│ └── __init__.py
├── project_B
│ ├── README.md
│ ├── src
│ │ ├── file_B.py
│ │ └── __init__.py
│ └── test
│ └── unittests.py
Within src/file_B.py
, I tried importing the class from file_A.py
, but received the following error
from ..project_A.file_A import class_A
ValueError: attempted relative import beyond top-level package
This being said, do I need to specify a reference in one of the __init__.py
files? Do __init__.py
files need to be created elsewhere for this to work?
python class import module init
add a comment |
up vote
0
down vote
favorite
During development, I want to import a class from file_A.py
into file_B.py
, without using the sys.path
method described here. For reference, my system specs and file tree are below.
OS: Ubuntu 18.04
Python: 3.6.4
parent_folder/
├── project_A
│ ├── file_A.py
│ └── __init__.py
├── project_B
│ ├── README.md
│ ├── src
│ │ ├── file_B.py
│ │ └── __init__.py
│ └── test
│ └── unittests.py
Within src/file_B.py
, I tried importing the class from file_A.py
, but received the following error
from ..project_A.file_A import class_A
ValueError: attempted relative import beyond top-level package
This being said, do I need to specify a reference in one of the __init__.py
files? Do __init__.py
files need to be created elsewhere for this to work?
python class import module init
Have you looked at this thread ?
– IMCoins
Nov 21 at 18:20
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
During development, I want to import a class from file_A.py
into file_B.py
, without using the sys.path
method described here. For reference, my system specs and file tree are below.
OS: Ubuntu 18.04
Python: 3.6.4
parent_folder/
├── project_A
│ ├── file_A.py
│ └── __init__.py
├── project_B
│ ├── README.md
│ ├── src
│ │ ├── file_B.py
│ │ └── __init__.py
│ └── test
│ └── unittests.py
Within src/file_B.py
, I tried importing the class from file_A.py
, but received the following error
from ..project_A.file_A import class_A
ValueError: attempted relative import beyond top-level package
This being said, do I need to specify a reference in one of the __init__.py
files? Do __init__.py
files need to be created elsewhere for this to work?
python class import module init
During development, I want to import a class from file_A.py
into file_B.py
, without using the sys.path
method described here. For reference, my system specs and file tree are below.
OS: Ubuntu 18.04
Python: 3.6.4
parent_folder/
├── project_A
│ ├── file_A.py
│ └── __init__.py
├── project_B
│ ├── README.md
│ ├── src
│ │ ├── file_B.py
│ │ └── __init__.py
│ └── test
│ └── unittests.py
Within src/file_B.py
, I tried importing the class from file_A.py
, but received the following error
from ..project_A.file_A import class_A
ValueError: attempted relative import beyond top-level package
This being said, do I need to specify a reference in one of the __init__.py
files? Do __init__.py
files need to be created elsewhere for this to work?
python class import module init
python class import module init
asked Nov 21 at 17:40
Greg
1713
1713
Have you looked at this thread ?
– IMCoins
Nov 21 at 18:20
add a comment |
Have you looked at this thread ?
– IMCoins
Nov 21 at 18:20
Have you looked at this thread ?
– IMCoins
Nov 21 at 18:20
Have you looked at this thread ?
– IMCoins
Nov 21 at 18:20
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417768%2fimporting-classes-modules-with-init-py%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you looked at this thread ?
– IMCoins
Nov 21 at 18:20