Python sub folder modules not able to import other subfolder modules
I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.
Parent Directory
---->lib
|--->module_group
|module_one.py(that tries but fails to import module_one)
|module_two.py
|main.py
In main I run
from lib.module_group.module_one import Module_One
Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.
from module_two import Module_Two
python-3.x import
add a comment |
I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.
Parent Directory
---->lib
|--->module_group
|module_one.py(that tries but fails to import module_one)
|module_two.py
|main.py
In main I run
from lib.module_group.module_one import Module_One
Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.
from module_two import Module_Two
python-3.x import
add a comment |
I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.
Parent Directory
---->lib
|--->module_group
|module_one.py(that tries but fails to import module_one)
|module_two.py
|main.py
In main I run
from lib.module_group.module_one import Module_One
Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.
from module_two import Module_Two
python-3.x import
I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.
Parent Directory
---->lib
|--->module_group
|module_one.py(that tries but fails to import module_one)
|module_two.py
|main.py
In main I run
from lib.module_group.module_one import Module_One
Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.
from module_two import Module_Two
python-3.x import
python-3.x import
asked Nov 26 '18 at 0:23
jake_314jake_314
308
308
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Set PYTHONPATH
to the directory you want to act as your root. For example, if you want to use the following code:
from lib.module_group.module_one import Module_One
Then set PYTHONPATH
to the directorying containing lib
. For example:
lib/module_group/module_one.py
:
from lib.module_group.module_two import Module_Two
lib/module_group/module_two.py
:
class Module_Two:
print('Loaded Module_Two')
Then, to run module_one.py
directly and still enable it to use lib.module_group.module_two
to load Module_Two
, use something like:
$ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
Loaded Module_Two
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53473369%2fpython-sub-folder-modules-not-able-to-import-other-subfolder-modules%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Set PYTHONPATH
to the directory you want to act as your root. For example, if you want to use the following code:
from lib.module_group.module_one import Module_One
Then set PYTHONPATH
to the directorying containing lib
. For example:
lib/module_group/module_one.py
:
from lib.module_group.module_two import Module_Two
lib/module_group/module_two.py
:
class Module_Two:
print('Loaded Module_Two')
Then, to run module_one.py
directly and still enable it to use lib.module_group.module_two
to load Module_Two
, use something like:
$ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
Loaded Module_Two
add a comment |
Set PYTHONPATH
to the directory you want to act as your root. For example, if you want to use the following code:
from lib.module_group.module_one import Module_One
Then set PYTHONPATH
to the directorying containing lib
. For example:
lib/module_group/module_one.py
:
from lib.module_group.module_two import Module_Two
lib/module_group/module_two.py
:
class Module_Two:
print('Loaded Module_Two')
Then, to run module_one.py
directly and still enable it to use lib.module_group.module_two
to load Module_Two
, use something like:
$ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
Loaded Module_Two
add a comment |
Set PYTHONPATH
to the directory you want to act as your root. For example, if you want to use the following code:
from lib.module_group.module_one import Module_One
Then set PYTHONPATH
to the directorying containing lib
. For example:
lib/module_group/module_one.py
:
from lib.module_group.module_two import Module_Two
lib/module_group/module_two.py
:
class Module_Two:
print('Loaded Module_Two')
Then, to run module_one.py
directly and still enable it to use lib.module_group.module_two
to load Module_Two
, use something like:
$ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
Loaded Module_Two
Set PYTHONPATH
to the directory you want to act as your root. For example, if you want to use the following code:
from lib.module_group.module_one import Module_One
Then set PYTHONPATH
to the directorying containing lib
. For example:
lib/module_group/module_one.py
:
from lib.module_group.module_two import Module_Two
lib/module_group/module_two.py
:
class Module_Two:
print('Loaded Module_Two')
Then, to run module_one.py
directly and still enable it to use lib.module_group.module_two
to load Module_Two
, use something like:
$ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
Loaded Module_Two
answered Nov 27 '18 at 19:14
binkibinki
3,30322953
3,30322953
add a comment |
add a 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.
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%2f53473369%2fpython-sub-folder-modules-not-able-to-import-other-subfolder-modules%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