The relative import feature of Python3 is not compatible with Jupyter or REPL, how to fix it?
Assuming if I have 2 files:
in something.py:
def a():
<... do something>
in b.py:
from .something import *
a()
then b.py theoretically should work when being executed as a module, thanks to the relative import feature (How to do relative imports in Python?)
However, when b.py is being evaluated in REPL (using eval() function), or b.py is converted to a jupyter notebook. Running will simply triggers:
ModuleNotFoundError: No module named 'main.something'; 'main' is not a package
This is very annoying: it basically means jupyter & python file have to use 2 versions to do the same thing, is there a way for relative import that works in all 3 cases?
python python-3.x jupyter read-eval-print-loop relative-import
add a comment |
Assuming if I have 2 files:
in something.py:
def a():
<... do something>
in b.py:
from .something import *
a()
then b.py theoretically should work when being executed as a module, thanks to the relative import feature (How to do relative imports in Python?)
However, when b.py is being evaluated in REPL (using eval() function), or b.py is converted to a jupyter notebook. Running will simply triggers:
ModuleNotFoundError: No module named 'main.something'; 'main' is not a package
This is very annoying: it basically means jupyter & python file have to use 2 versions to do the same thing, is there a way for relative import that works in all 3 cases?
python python-3.x jupyter read-eval-print-loop relative-import
Not sure about the specifics of how your code is structured, but your question might possibly a duplicate of this: stackoverflow.com/questions/34478398/…; basically, hack your module path so that it includes the directory that contains your code, and then import your code like a module.
– dtanabe
Jan 19 at 2:45
Agreed, but there are many different ways to hack a REPL/jupyter's module path, I'm simply looking for a concise and widely-accepted solution
– tribbloid
Jan 19 at 2:52
What exactly do you mean byb.py is being evaluated in REPL? Oh, and that 'recently introduced' link? It's from 2008.
– hpaulj
Jan 19 at 2:58
@hpaulj yeah you are right, I thought it was introduced in python 3, turns out that it's already there for a long time, just not popular
– tribbloid
Jan 19 at 19:10
This problem has nothing to do with relative imports. You would have the exact same problem with absolute imports.
– user2357112
Jan 19 at 19:17
add a comment |
Assuming if I have 2 files:
in something.py:
def a():
<... do something>
in b.py:
from .something import *
a()
then b.py theoretically should work when being executed as a module, thanks to the relative import feature (How to do relative imports in Python?)
However, when b.py is being evaluated in REPL (using eval() function), or b.py is converted to a jupyter notebook. Running will simply triggers:
ModuleNotFoundError: No module named 'main.something'; 'main' is not a package
This is very annoying: it basically means jupyter & python file have to use 2 versions to do the same thing, is there a way for relative import that works in all 3 cases?
python python-3.x jupyter read-eval-print-loop relative-import
Assuming if I have 2 files:
in something.py:
def a():
<... do something>
in b.py:
from .something import *
a()
then b.py theoretically should work when being executed as a module, thanks to the relative import feature (How to do relative imports in Python?)
However, when b.py is being evaluated in REPL (using eval() function), or b.py is converted to a jupyter notebook. Running will simply triggers:
ModuleNotFoundError: No module named 'main.something'; 'main' is not a package
This is very annoying: it basically means jupyter & python file have to use 2 versions to do the same thing, is there a way for relative import that works in all 3 cases?
python python-3.x jupyter read-eval-print-loop relative-import
python python-3.x jupyter read-eval-print-loop relative-import
edited Jan 19 at 19:10
tribbloid
asked Jan 19 at 2:37
tribbloidtribbloid
1,92052751
1,92052751
Not sure about the specifics of how your code is structured, but your question might possibly a duplicate of this: stackoverflow.com/questions/34478398/…; basically, hack your module path so that it includes the directory that contains your code, and then import your code like a module.
– dtanabe
Jan 19 at 2:45
Agreed, but there are many different ways to hack a REPL/jupyter's module path, I'm simply looking for a concise and widely-accepted solution
– tribbloid
Jan 19 at 2:52
What exactly do you mean byb.py is being evaluated in REPL? Oh, and that 'recently introduced' link? It's from 2008.
– hpaulj
Jan 19 at 2:58
@hpaulj yeah you are right, I thought it was introduced in python 3, turns out that it's already there for a long time, just not popular
– tribbloid
Jan 19 at 19:10
This problem has nothing to do with relative imports. You would have the exact same problem with absolute imports.
– user2357112
Jan 19 at 19:17
add a comment |
Not sure about the specifics of how your code is structured, but your question might possibly a duplicate of this: stackoverflow.com/questions/34478398/…; basically, hack your module path so that it includes the directory that contains your code, and then import your code like a module.
– dtanabe
Jan 19 at 2:45
Agreed, but there are many different ways to hack a REPL/jupyter's module path, I'm simply looking for a concise and widely-accepted solution
– tribbloid
Jan 19 at 2:52
What exactly do you mean byb.py is being evaluated in REPL? Oh, and that 'recently introduced' link? It's from 2008.
– hpaulj
Jan 19 at 2:58
@hpaulj yeah you are right, I thought it was introduced in python 3, turns out that it's already there for a long time, just not popular
– tribbloid
Jan 19 at 19:10
This problem has nothing to do with relative imports. You would have the exact same problem with absolute imports.
– user2357112
Jan 19 at 19:17
Not sure about the specifics of how your code is structured, but your question might possibly a duplicate of this: stackoverflow.com/questions/34478398/…; basically, hack your module path so that it includes the directory that contains your code, and then import your code like a module.
– dtanabe
Jan 19 at 2:45
Not sure about the specifics of how your code is structured, but your question might possibly a duplicate of this: stackoverflow.com/questions/34478398/…; basically, hack your module path so that it includes the directory that contains your code, and then import your code like a module.
– dtanabe
Jan 19 at 2:45
Agreed, but there are many different ways to hack a REPL/jupyter's module path, I'm simply looking for a concise and widely-accepted solution
– tribbloid
Jan 19 at 2:52
Agreed, but there are many different ways to hack a REPL/jupyter's module path, I'm simply looking for a concise and widely-accepted solution
– tribbloid
Jan 19 at 2:52
What exactly do you mean by
b.py is being evaluated in REPL? Oh, and that 'recently introduced' link? It's from 2008.– hpaulj
Jan 19 at 2:58
What exactly do you mean by
b.py is being evaluated in REPL? Oh, and that 'recently introduced' link? It's from 2008.– hpaulj
Jan 19 at 2:58
@hpaulj yeah you are right, I thought it was introduced in python 3, turns out that it's already there for a long time, just not popular
– tribbloid
Jan 19 at 19:10
@hpaulj yeah you are right, I thought it was introduced in python 3, turns out that it's already there for a long time, just not popular
– tribbloid
Jan 19 at 19:10
This problem has nothing to do with relative imports. You would have the exact same problem with absolute imports.
– user2357112
Jan 19 at 19:17
This problem has nothing to do with relative imports. You would have the exact same problem with absolute imports.
– user2357112
Jan 19 at 19:17
add a comment |
0
active
oldest
votes
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%2f54263628%2fthe-relative-import-feature-of-python3-is-not-compatible-with-jupyter-or-repl-h%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
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.
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%2f54263628%2fthe-relative-import-feature-of-python3-is-not-compatible-with-jupyter-or-repl-h%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
Not sure about the specifics of how your code is structured, but your question might possibly a duplicate of this: stackoverflow.com/questions/34478398/…; basically, hack your module path so that it includes the directory that contains your code, and then import your code like a module.
– dtanabe
Jan 19 at 2:45
Agreed, but there are many different ways to hack a REPL/jupyter's module path, I'm simply looking for a concise and widely-accepted solution
– tribbloid
Jan 19 at 2:52
What exactly do you mean by
b.py is being evaluated in REPL? Oh, and that 'recently introduced' link? It's from 2008.– hpaulj
Jan 19 at 2:58
@hpaulj yeah you are right, I thought it was introduced in python 3, turns out that it's already there for a long time, just not popular
– tribbloid
Jan 19 at 19:10
This problem has nothing to do with relative imports. You would have the exact same problem with absolute imports.
– user2357112
Jan 19 at 19:17