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'









share|improve this question




















  • 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

















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'









share|improve this question




















  • 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















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'









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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










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














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





share|improve this answer





















  • 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


















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






share|improve this answer























  • 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 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










  • @임지웅 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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





share|improve this answer





















  • 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















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





share|improve this answer





















  • 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













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





share|improve this answer












Change file directory:



import os, sys   
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),"../../project")))
from _common import A






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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












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






share|improve this answer























  • 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 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










  • @임지웅 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















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






share|improve this answer























  • 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 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










  • @임지웅 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













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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 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










  • @임지웅 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


















  • 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 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










  • @임지웅 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...