Python : how do i import variable from one higher directory?
up vote
0
down vote
favorite
How do I import from a higher level directory in python?
For example, I have:
/var/www/PROJECT/subproject/_common.py
/var/www/PROJECT/subproject/stuff/routes.py
I want to import variable A
in _common.py
to routes.py
# routes.py
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from _common import A
but I get the error:
ImportError:cannot import name 'A'
python importerror
|
show 1 more comment
up vote
0
down vote
favorite
How do I import from a higher level directory in python?
For example, I have:
/var/www/PROJECT/subproject/_common.py
/var/www/PROJECT/subproject/stuff/routes.py
I want to import variable A
in _common.py
to routes.py
# routes.py
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from _common import A
but I get the error:
ImportError:cannot import name 'A'
python importerror
1
Have you check this link ? stackoverflow.com/questions/4383571/…
– Avichal Bettoli
Nov 21 at 11:32
Did you includeproject/__init__.py
file?
– Cartucho
Nov 21 at 11:37
@Cartucho yes i have project/__init__.py file
– 임지웅
Nov 21 at 11:51
@AvichalBettoli most of them i tried. i also tried from .._common import A but it gives import beyond top-level package error
– 임지웅
Nov 21 at 11:52
Maybe replacing".."
by"../.."
?
– Cartucho
Nov 21 at 11:58
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How do I import from a higher level directory in python?
For example, I have:
/var/www/PROJECT/subproject/_common.py
/var/www/PROJECT/subproject/stuff/routes.py
I want to import variable A
in _common.py
to routes.py
# routes.py
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from _common import A
but I get the error:
ImportError:cannot import name 'A'
python importerror
How do I import from a higher level directory in python?
For example, I have:
/var/www/PROJECT/subproject/_common.py
/var/www/PROJECT/subproject/stuff/routes.py
I want to import variable A
in _common.py
to routes.py
# routes.py
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from _common import A
but I get the error:
ImportError:cannot import name 'A'
python importerror
python importerror
edited Nov 21 at 13:15
asked Nov 21 at 11:27
임지웅
33
33
1
Have you check this link ? stackoverflow.com/questions/4383571/…
– Avichal Bettoli
Nov 21 at 11:32
Did you includeproject/__init__.py
file?
– Cartucho
Nov 21 at 11:37
@Cartucho yes i have project/__init__.py file
– 임지웅
Nov 21 at 11:51
@AvichalBettoli most of them i tried. i also tried from .._common import A but it gives import beyond top-level package error
– 임지웅
Nov 21 at 11:52
Maybe replacing".."
by"../.."
?
– Cartucho
Nov 21 at 11:58
|
show 1 more comment
1
Have you check this link ? stackoverflow.com/questions/4383571/…
– Avichal Bettoli
Nov 21 at 11:32
Did you includeproject/__init__.py
file?
– Cartucho
Nov 21 at 11:37
@Cartucho yes i have project/__init__.py file
– 임지웅
Nov 21 at 11:51
@AvichalBettoli most of them i tried. i also tried from .._common import A but it gives import beyond top-level package error
– 임지웅
Nov 21 at 11:52
Maybe replacing".."
by"../.."
?
– Cartucho
Nov 21 at 11:58
1
1
Have you check this link ? stackoverflow.com/questions/4383571/…
– Avichal Bettoli
Nov 21 at 11:32
Have you check this link ? stackoverflow.com/questions/4383571/…
– Avichal Bettoli
Nov 21 at 11:32
Did you include
project/__init__.py
file?– Cartucho
Nov 21 at 11:37
Did you include
project/__init__.py
file?– Cartucho
Nov 21 at 11:37
@Cartucho yes i have project/__init__.py file
– 임지웅
Nov 21 at 11:51
@Cartucho yes i have project/__init__.py file
– 임지웅
Nov 21 at 11:51
@AvichalBettoli most of them i tried. i also tried from .._common import A but it gives import beyond top-level package error
– 임지웅
Nov 21 at 11:52
@AvichalBettoli most of them i tried. i also tried from .._common import A but it gives import beyond top-level package error
– 임지웅
Nov 21 at 11:52
Maybe replacing
".."
by "../.."
?– Cartucho
Nov 21 at 11:58
Maybe replacing
".."
by "../.."
?– Cartucho
Nov 21 at 11:58
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
Change file directory:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../../project")))
from _common import A
i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
You have to go from routes.py to two top file paths. You enter PROJECT after the two files are up. And enter the subproject directory and you can import import _common.py. Path = "../../subproject". If still not working, make sure it has A function in _common. @임지웅
– Ali.Turkkan
Nov 21 at 13:34
As I understand it, A is not a function. A is a variable. You can't import a variable. Your create a functioan and return A variable. After import and use A variable. @임지웅
– Ali.Turkkan
Nov 21 at 13:45
add a comment |
up vote
0
down vote
OLD VERSION
To solve the issue replace ".."
with os.pardir
:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
from _common import A
NEW VERSION
The code above does not solve the problem in the question because the true problem lies in the project structure not in the particular line. The problem is circular import. The problem became clear after the full traceback has been provided. Here is the simple way to reproduce the issue - consider 3 files...
main.py:
import a
a.py:
import b
A = 'A'
b.py:
from a import A
... the error is:
ImportError: cannot import name 'A'
OR
b.py:
import a
BB = a.A
... the error is:
AttributeError: module 'a' has no attribute 'A'
The solution to the problem has been discussed many times - search on SO
same here, i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
@임지웅 Does your_common.py
script really containA
? MaybeA
is located inside some function or conditional structure? Can you provide the code of_common.py
?
– Poolka
Nov 21 at 13:33
from flask import Flask, render_template, url_for, redirect, request, Blueprint from flaskext.mysql import MySQL from init import app from db_controller.function import get_db import os, sys A = get_db('mysql')
– 임지웅
Nov 21 at 13:38
@임지웅 I tested the code with_common.py
containing a single lineA = "this is A"
. And the code works. Try this very simple version. Also, please edit your question - add_common.py
content and full traceback for the error.
– Poolka
Nov 21 at 13:45
|
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Change file directory:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../../project")))
from _common import A
i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
You have to go from routes.py to two top file paths. You enter PROJECT after the two files are up. And enter the subproject directory and you can import import _common.py. Path = "../../subproject". If still not working, make sure it has A function in _common. @임지웅
– Ali.Turkkan
Nov 21 at 13:34
As I understand it, A is not a function. A is a variable. You can't import a variable. Your create a functioan and return A variable. After import and use A variable. @임지웅
– Ali.Turkkan
Nov 21 at 13:45
add a comment |
up vote
0
down vote
Change file directory:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../../project")))
from _common import A
i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
You have to go from routes.py to two top file paths. You enter PROJECT after the two files are up. And enter the subproject directory and you can import import _common.py. Path = "../../subproject". If still not working, make sure it has A function in _common. @임지웅
– Ali.Turkkan
Nov 21 at 13:34
As I understand it, A is not a function. A is a variable. You can't import a variable. Your create a functioan and return A variable. After import and use A variable. @임지웅
– Ali.Turkkan
Nov 21 at 13:45
add a comment |
up vote
0
down vote
up vote
0
down vote
Change file directory:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../../project")))
from _common import A
Change file directory:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../../project")))
from _common import A
answered Nov 21 at 12:20
Ali.Turkkan
757
757
i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
You have to go from routes.py to two top file paths. You enter PROJECT after the two files are up. And enter the subproject directory and you can import import _common.py. Path = "../../subproject". If still not working, make sure it has A function in _common. @임지웅
– Ali.Turkkan
Nov 21 at 13:34
As I understand it, A is not a function. A is a variable. You can't import a variable. Your create a functioan and return A variable. After import and use A variable. @임지웅
– Ali.Turkkan
Nov 21 at 13:45
add a comment |
i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
You have to go from routes.py to two top file paths. You enter PROJECT after the two files are up. And enter the subproject directory and you can import import _common.py. Path = "../../subproject". If still not working, make sure it has A function in _common. @임지웅
– Ali.Turkkan
Nov 21 at 13:34
As I understand it, A is not a function. A is a variable. You can't import a variable. Your create a functioan and return A variable. After import and use A variable. @임지웅
– Ali.Turkkan
Nov 21 at 13:45
i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
You have to go from routes.py to two top file paths. You enter PROJECT after the two files are up. And enter the subproject directory and you can import import _common.py. Path = "../../subproject". If still not working, make sure it has A function in _common. @임지웅
– Ali.Turkkan
Nov 21 at 13:34
You have to go from routes.py to two top file paths. You enter PROJECT after the two files are up. And enter the subproject directory and you can import import _common.py. Path = "../../subproject". If still not working, make sure it has A function in _common. @임지웅
– Ali.Turkkan
Nov 21 at 13:34
As I understand it, A is not a function. A is a variable. You can't import a variable. Your create a functioan and return A variable. After import and use A variable. @임지웅
– Ali.Turkkan
Nov 21 at 13:45
As I understand it, A is not a function. A is a variable. You can't import a variable. Your create a functioan and return A variable. After import and use A variable. @임지웅
– Ali.Turkkan
Nov 21 at 13:45
add a comment |
up vote
0
down vote
OLD VERSION
To solve the issue replace ".."
with os.pardir
:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
from _common import A
NEW VERSION
The code above does not solve the problem in the question because the true problem lies in the project structure not in the particular line. The problem is circular import. The problem became clear after the full traceback has been provided. Here is the simple way to reproduce the issue - consider 3 files...
main.py:
import a
a.py:
import b
A = 'A'
b.py:
from a import A
... the error is:
ImportError: cannot import name 'A'
OR
b.py:
import a
BB = a.A
... the error is:
AttributeError: module 'a' has no attribute 'A'
The solution to the problem has been discussed many times - search on SO
same here, i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
@임지웅 Does your_common.py
script really containA
? MaybeA
is located inside some function or conditional structure? Can you provide the code of_common.py
?
– Poolka
Nov 21 at 13:33
from flask import Flask, render_template, url_for, redirect, request, Blueprint from flaskext.mysql import MySQL from init import app from db_controller.function import get_db import os, sys A = get_db('mysql')
– 임지웅
Nov 21 at 13:38
@임지웅 I tested the code with_common.py
containing a single lineA = "this is A"
. And the code works. Try this very simple version. Also, please edit your question - add_common.py
content and full traceback for the error.
– Poolka
Nov 21 at 13:45
|
show 1 more comment
up vote
0
down vote
OLD VERSION
To solve the issue replace ".."
with os.pardir
:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
from _common import A
NEW VERSION
The code above does not solve the problem in the question because the true problem lies in the project structure not in the particular line. The problem is circular import. The problem became clear after the full traceback has been provided. Here is the simple way to reproduce the issue - consider 3 files...
main.py:
import a
a.py:
import b
A = 'A'
b.py:
from a import A
... the error is:
ImportError: cannot import name 'A'
OR
b.py:
import a
BB = a.A
... the error is:
AttributeError: module 'a' has no attribute 'A'
The solution to the problem has been discussed many times - search on SO
same here, i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
@임지웅 Does your_common.py
script really containA
? MaybeA
is located inside some function or conditional structure? Can you provide the code of_common.py
?
– Poolka
Nov 21 at 13:33
from flask import Flask, render_template, url_for, redirect, request, Blueprint from flaskext.mysql import MySQL from init import app from db_controller.function import get_db import os, sys A = get_db('mysql')
– 임지웅
Nov 21 at 13:38
@임지웅 I tested the code with_common.py
containing a single lineA = "this is A"
. And the code works. Try this very simple version. Also, please edit your question - add_common.py
content and full traceback for the error.
– Poolka
Nov 21 at 13:45
|
show 1 more comment
up vote
0
down vote
up vote
0
down vote
OLD VERSION
To solve the issue replace ".."
with os.pardir
:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
from _common import A
NEW VERSION
The code above does not solve the problem in the question because the true problem lies in the project structure not in the particular line. The problem is circular import. The problem became clear after the full traceback has been provided. Here is the simple way to reproduce the issue - consider 3 files...
main.py:
import a
a.py:
import b
A = 'A'
b.py:
from a import A
... the error is:
ImportError: cannot import name 'A'
OR
b.py:
import a
BB = a.A
... the error is:
AttributeError: module 'a' has no attribute 'A'
The solution to the problem has been discussed many times - search on SO
OLD VERSION
To solve the issue replace ".."
with os.pardir
:
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
from _common import A
NEW VERSION
The code above does not solve the problem in the question because the true problem lies in the project structure not in the particular line. The problem is circular import. The problem became clear after the full traceback has been provided. Here is the simple way to reproduce the issue - consider 3 files...
main.py:
import a
a.py:
import b
A = 'A'
b.py:
from a import A
... the error is:
ImportError: cannot import name 'A'
OR
b.py:
import a
BB = a.A
... the error is:
AttributeError: module 'a' has no attribute 'A'
The solution to the problem has been discussed many times - search on SO
edited Nov 21 at 16:01
answered Nov 21 at 12:20
Poolka
1,159128
1,159128
same here, i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
@임지웅 Does your_common.py
script really containA
? MaybeA
is located inside some function or conditional structure? Can you provide the code of_common.py
?
– Poolka
Nov 21 at 13:33
from flask import Flask, render_template, url_for, redirect, request, Blueprint from flaskext.mysql import MySQL from init import app from db_controller.function import get_db import os, sys A = get_db('mysql')
– 임지웅
Nov 21 at 13:38
@임지웅 I tested the code with_common.py
containing a single lineA = "this is A"
. And the code works. Try this very simple version. Also, please edit your question - add_common.py
content and full traceback for the error.
– Poolka
Nov 21 at 13:45
|
show 1 more comment
same here, i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
@임지웅 Does your_common.py
script really containA
? MaybeA
is located inside some function or conditional structure? Can you provide the code of_common.py
?
– Poolka
Nov 21 at 13:33
from flask import Flask, render_template, url_for, redirect, request, Blueprint from flaskext.mysql import MySQL from init import app from db_controller.function import get_db import os, sys A = get_db('mysql')
– 임지웅
Nov 21 at 13:38
@임지웅 I tested the code with_common.py
containing a single lineA = "this is A"
. And the code works. Try this very simple version. Also, please edit your question - add_common.py
content and full traceback for the error.
– Poolka
Nov 21 at 13:45
same here, i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
same here, i tried, still import error saying cannot import name 'A'
– 임지웅
Nov 21 at 13:14
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
edited to my full directory plz help
– 임지웅
Nov 21 at 13:16
@임지웅 Does your
_common.py
script really contain A
? Maybe A
is located inside some function or conditional structure? Can you provide the code of _common.py
?– Poolka
Nov 21 at 13:33
@임지웅 Does your
_common.py
script really contain A
? Maybe A
is located inside some function or conditional structure? Can you provide the code of _common.py
?– Poolka
Nov 21 at 13:33
from flask import Flask, render_template, url_for, redirect, request, Blueprint from flaskext.mysql import MySQL from init import app from db_controller.function import get_db import os, sys A = get_db('mysql')
– 임지웅
Nov 21 at 13:38
from flask import Flask, render_template, url_for, redirect, request, Blueprint from flaskext.mysql import MySQL from init import app from db_controller.function import get_db import os, sys A = get_db('mysql')
– 임지웅
Nov 21 at 13:38
@임지웅 I tested the code with
_common.py
containing a single line A = "this is A"
. And the code works. Try this very simple version. Also, please edit your question - add _common.py
content and full traceback for the error.– Poolka
Nov 21 at 13:45
@임지웅 I tested the code with
_common.py
containing a single line A = "this is A"
. And the code works. Try this very simple version. Also, please edit your question - add _common.py
content and full traceback for the error.– Poolka
Nov 21 at 13:45
|
show 1 more comment
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%2f53411107%2fpython-how-do-i-import-variable-from-one-higher-directory%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
1
Have you check this link ? stackoverflow.com/questions/4383571/…
– Avichal Bettoli
Nov 21 at 11:32
Did you include
project/__init__.py
file?– Cartucho
Nov 21 at 11:37
@Cartucho yes i have project/__init__.py file
– 임지웅
Nov 21 at 11:51
@AvichalBettoli most of them i tried. i also tried from .._common import A but it gives import beyond top-level package error
– 임지웅
Nov 21 at 11:52
Maybe replacing
".."
by"../.."
?– Cartucho
Nov 21 at 11:58