Relative imports - ModuleNotFoundError: No module named x
This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files:
- test.py
- config.py
config.py has a few functions defined in it as well as a few variables. I've stripped it down to the following:
However, I'm getting the following error:
ModuleNotFoundError: No module named 'config'
I'm aware that the py3 convention is to use absolute imports:
from . import config
However, this leads to the following error:
ImportError: cannot import name 'config'
So I'm at a loss as to what to do here... Any help is greatly appreciated. :)
python python-3.x
|
show 2 more comments
This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files:
- test.py
- config.py
config.py has a few functions defined in it as well as a few variables. I've stripped it down to the following:
However, I'm getting the following error:
ModuleNotFoundError: No module named 'config'
I'm aware that the py3 convention is to use absolute imports:
from . import config
However, this leads to the following error:
ImportError: cannot import name 'config'
So I'm at a loss as to what to do here... Any help is greatly appreciated. :)
python python-3.x
I cannot reproduce the error, how do you execute this code?
– Copperfield
May 5 '17 at 1:57
I executetest.py
through pyCharm with Python 3.6. Does yours execute fine?
– Ryan
May 5 '17 at 3:18
1
I execute it with idle that come with python, and also aspython test.py
, and it work perfectly fine. I don't have pyCharm, but perhaps is some bad configuration of pyCharm that is causing the problem
– Copperfield
May 5 '17 at 4:03
1
Very odd. I'm using WinPython - just download vanilla Python 3.6 from python.org, and it works fine. Never thought to check the interpreter! Thanks!
– Ryan
May 5 '17 at 5:17
1
My guess is that something funky is going on with PYTHONPATH. Check your IDE settings and/or system environment variables.
– Martin Tournoij
May 6 '17 at 19:53
|
show 2 more comments
This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files:
- test.py
- config.py
config.py has a few functions defined in it as well as a few variables. I've stripped it down to the following:
However, I'm getting the following error:
ModuleNotFoundError: No module named 'config'
I'm aware that the py3 convention is to use absolute imports:
from . import config
However, this leads to the following error:
ImportError: cannot import name 'config'
So I'm at a loss as to what to do here... Any help is greatly appreciated. :)
python python-3.x
This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files:
- test.py
- config.py
config.py has a few functions defined in it as well as a few variables. I've stripped it down to the following:
However, I'm getting the following error:
ModuleNotFoundError: No module named 'config'
I'm aware that the py3 convention is to use absolute imports:
from . import config
However, this leads to the following error:
ImportError: cannot import name 'config'
So I'm at a loss as to what to do here... Any help is greatly appreciated. :)
python python-3.x
python python-3.x
edited Dec 5 at 8:00
cricket_007
78.9k1142109
78.9k1142109
asked May 2 '17 at 0:40
Ryan
1,66831723
1,66831723
I cannot reproduce the error, how do you execute this code?
– Copperfield
May 5 '17 at 1:57
I executetest.py
through pyCharm with Python 3.6. Does yours execute fine?
– Ryan
May 5 '17 at 3:18
1
I execute it with idle that come with python, and also aspython test.py
, and it work perfectly fine. I don't have pyCharm, but perhaps is some bad configuration of pyCharm that is causing the problem
– Copperfield
May 5 '17 at 4:03
1
Very odd. I'm using WinPython - just download vanilla Python 3.6 from python.org, and it works fine. Never thought to check the interpreter! Thanks!
– Ryan
May 5 '17 at 5:17
1
My guess is that something funky is going on with PYTHONPATH. Check your IDE settings and/or system environment variables.
– Martin Tournoij
May 6 '17 at 19:53
|
show 2 more comments
I cannot reproduce the error, how do you execute this code?
– Copperfield
May 5 '17 at 1:57
I executetest.py
through pyCharm with Python 3.6. Does yours execute fine?
– Ryan
May 5 '17 at 3:18
1
I execute it with idle that come with python, and also aspython test.py
, and it work perfectly fine. I don't have pyCharm, but perhaps is some bad configuration of pyCharm that is causing the problem
– Copperfield
May 5 '17 at 4:03
1
Very odd. I'm using WinPython - just download vanilla Python 3.6 from python.org, and it works fine. Never thought to check the interpreter! Thanks!
– Ryan
May 5 '17 at 5:17
1
My guess is that something funky is going on with PYTHONPATH. Check your IDE settings and/or system environment variables.
– Martin Tournoij
May 6 '17 at 19:53
I cannot reproduce the error, how do you execute this code?
– Copperfield
May 5 '17 at 1:57
I cannot reproduce the error, how do you execute this code?
– Copperfield
May 5 '17 at 1:57
I execute
test.py
through pyCharm with Python 3.6. Does yours execute fine?– Ryan
May 5 '17 at 3:18
I execute
test.py
through pyCharm with Python 3.6. Does yours execute fine?– Ryan
May 5 '17 at 3:18
1
1
I execute it with idle that come with python, and also as
python test.py
, and it work perfectly fine. I don't have pyCharm, but perhaps is some bad configuration of pyCharm that is causing the problem– Copperfield
May 5 '17 at 4:03
I execute it with idle that come with python, and also as
python test.py
, and it work perfectly fine. I don't have pyCharm, but perhaps is some bad configuration of pyCharm that is causing the problem– Copperfield
May 5 '17 at 4:03
1
1
Very odd. I'm using WinPython - just download vanilla Python 3.6 from python.org, and it works fine. Never thought to check the interpreter! Thanks!
– Ryan
May 5 '17 at 5:17
Very odd. I'm using WinPython - just download vanilla Python 3.6 from python.org, and it works fine. Never thought to check the interpreter! Thanks!
– Ryan
May 5 '17 at 5:17
1
1
My guess is that something funky is going on with PYTHONPATH. Check your IDE settings and/or system environment variables.
– Martin Tournoij
May 6 '17 at 19:53
My guess is that something funky is going on with PYTHONPATH. Check your IDE settings and/or system environment variables.
– Martin Tournoij
May 6 '17 at 19:53
|
show 2 more comments
6 Answers
6
active
oldest
votes
TL;DR: you can't do relative imports from the file you execute since __main__
module is not a part of a package.
Absolute imports - import something available on sys.path
Relative imports - import something relative to the current module, must be a part of a package
If you're running both variants in exactly the same way, one of them should work. Anyway, here is an example that should help you understand what's going on, let's add another main.py
file with the overall directory structure like this:
.
./main.py
./ryan/__init__.py
./ryan/config.py
./ryan/test.py
And let's update test.py to see what's going on:
# config.py
debug = True
# test.py
print(__name__)
try:
# Trying to find module in the parent package
from . import config
print(config.debug)
del config
except ImportError:
print('Relative import failed')
try:
# Trying to find module on sys.path
import config
print(config.debug)
except ModuleNotFoundError:
print('Absolute import failed')
# main.py
import ryan.test
Let's run test.py first:
$ python ryan/test.py
__main__
Relative import failed
True
Here "test" is the __main__
module and doesn't know anything about belonging to a package. However import config
should work, since the ryan
folder will be added to sys.path.
Let's run main.py instead:
$ python main.py
ryan.test
True
Absolute import failed
And here test is inside of the "ryan" package and can perform relative imports. import config
fails since implicit relative imports are not allowed in Python 3.
Hope this helped.
P.S.: if you're sticking with Python 3 there is no more need in __init__.py
files.
2
Is there something I can do to make absolute imports always work? Like, callsys.path.append('/some/path/my_module')
inside of/some/path/my_module/__init__.py
?
– James T.
Dec 7 '17 at 19:22
1
@JamesT. Yes, it's pretty common to modifysys.path
during runtime (github.com/…). You can also set PYTHONPATH environment variable.
– Igonato
Dec 8 '17 at 4:32
add a comment |
I figured it out. Very frustrating, especially coming from python2.
You have to add a .
to the module, regardless of whether or not it is relative or absolute.
I created the directory setup as follows.
/main.py
--/lib
--/__init__.py
--/mody.py
--/modx.py
modx.py
def does_something():
return "I gave you this string."
mody.py
from modx import does_something
def loaded():
string = does_something()
print(string)
main.py
from lib import mody
mody.loaded()
when I execute main, this is what happens
$ python main.py
Traceback (most recent call last):
File "main.py", line 2, in <module>
from lib import mody
File "/mnt/c/Users/Austin/Dropbox/Source/Python/virtualenviron/mock/package/lib/mody.py", line 1, in <module>
from modx import does_something
ImportError: No module named 'modx'
I ran 2to3, and the core output was this
RefactoringTool: Refactored lib/mody.py
--- lib/mody.py (original)
+++ lib/mody.py (refactored)
@@ -1,4 +1,4 @@
-from modx import does_something
+from .modx import does_something
def loaded():
string = does_something()
RefactoringTool: Files that need to be modified:
RefactoringTool: lib/modx.py
RefactoringTool: lib/mody.py
I had to modify mody.py's import statement to fix it
try:
from modx import does_something
except ImportError:
from .modx import does_something
def loaded():
string = does_something()
print(string)
Then I ran main.py again and got the expected output
$ python main.py
I gave you this string.
Lastly, just to clean it up and make it portable between 2 and 3.
from __future__ import absolute_import
from .modx import does_something
add a comment |
Tryed your example
from . import config
got the following SystemError:
/usr/bin/python3.4 test.py
Traceback (most recent call last):
File "test.py", line 1, in
from . import config
SystemError: Parent module '' not loaded, cannot perform relative import
This will work for me:
import config
print('debug=%s'%config.debug)
>>>debug=True
Tested with Python:3.4.2 - PyCharm 2016.3.2
Beside this PyCharm offers you to Import this name.
You hav to click on config
and a help icon appears.
add a comment |
Setting PYTHONPATH can also help with this problem.
Here is how it can be done. Please refer the line highlighted in green
2
setting PYTHONPATH to main code directory solved the problem for me!
– Geek
May 22 at 4:21
add a comment |
This example works on Python 3.6.
I suggest going to Run -> Edit Configurations
in PyCharm, deleting any entries there, and trying to run the code through PyCharm again.
If that doesn't work, check your project interpreter (Settings -> Project Interpreter) and run configuration defaults (Run -> Edit Configurations...).
add a comment |
As was stated in the comments to the original post, this seemed to be an issue with the python interpreter I was using for whatever reason, and not something wrong with the python scripts. I switched over from the WinPython bundle to the official python 3.6 from python.org and it worked just fine. thanks for the help everyone :)
Hmm hate to say this but same thing just happened to me. Recreating environment fix the issue. In my case, I was getting this error when running tests. In the same environment, attempt to import same module worked. Recreating environment fixed them all (same python version 3.6)
– naoko
Aug 1 '17 at 19:27
Different IDE's have different way of handling path's specially for project source files (views, modules, templates, etc.) If your project is structured and coded properly, then it should work to all (standard) IDE's. Having issues with popular IDE's like WinPython means the problem is indeed coming from your project. As mentioned above, the problem is "You have to add a . to the module" by user3159377 which should be the accepted answer.
– winux
Dec 12 at 8:41
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%2f43728431%2frelative-imports-modulenotfounderror-no-module-named-x%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
TL;DR: you can't do relative imports from the file you execute since __main__
module is not a part of a package.
Absolute imports - import something available on sys.path
Relative imports - import something relative to the current module, must be a part of a package
If you're running both variants in exactly the same way, one of them should work. Anyway, here is an example that should help you understand what's going on, let's add another main.py
file with the overall directory structure like this:
.
./main.py
./ryan/__init__.py
./ryan/config.py
./ryan/test.py
And let's update test.py to see what's going on:
# config.py
debug = True
# test.py
print(__name__)
try:
# Trying to find module in the parent package
from . import config
print(config.debug)
del config
except ImportError:
print('Relative import failed')
try:
# Trying to find module on sys.path
import config
print(config.debug)
except ModuleNotFoundError:
print('Absolute import failed')
# main.py
import ryan.test
Let's run test.py first:
$ python ryan/test.py
__main__
Relative import failed
True
Here "test" is the __main__
module and doesn't know anything about belonging to a package. However import config
should work, since the ryan
folder will be added to sys.path.
Let's run main.py instead:
$ python main.py
ryan.test
True
Absolute import failed
And here test is inside of the "ryan" package and can perform relative imports. import config
fails since implicit relative imports are not allowed in Python 3.
Hope this helped.
P.S.: if you're sticking with Python 3 there is no more need in __init__.py
files.
2
Is there something I can do to make absolute imports always work? Like, callsys.path.append('/some/path/my_module')
inside of/some/path/my_module/__init__.py
?
– James T.
Dec 7 '17 at 19:22
1
@JamesT. Yes, it's pretty common to modifysys.path
during runtime (github.com/…). You can also set PYTHONPATH environment variable.
– Igonato
Dec 8 '17 at 4:32
add a comment |
TL;DR: you can't do relative imports from the file you execute since __main__
module is not a part of a package.
Absolute imports - import something available on sys.path
Relative imports - import something relative to the current module, must be a part of a package
If you're running both variants in exactly the same way, one of them should work. Anyway, here is an example that should help you understand what's going on, let's add another main.py
file with the overall directory structure like this:
.
./main.py
./ryan/__init__.py
./ryan/config.py
./ryan/test.py
And let's update test.py to see what's going on:
# config.py
debug = True
# test.py
print(__name__)
try:
# Trying to find module in the parent package
from . import config
print(config.debug)
del config
except ImportError:
print('Relative import failed')
try:
# Trying to find module on sys.path
import config
print(config.debug)
except ModuleNotFoundError:
print('Absolute import failed')
# main.py
import ryan.test
Let's run test.py first:
$ python ryan/test.py
__main__
Relative import failed
True
Here "test" is the __main__
module and doesn't know anything about belonging to a package. However import config
should work, since the ryan
folder will be added to sys.path.
Let's run main.py instead:
$ python main.py
ryan.test
True
Absolute import failed
And here test is inside of the "ryan" package and can perform relative imports. import config
fails since implicit relative imports are not allowed in Python 3.
Hope this helped.
P.S.: if you're sticking with Python 3 there is no more need in __init__.py
files.
2
Is there something I can do to make absolute imports always work? Like, callsys.path.append('/some/path/my_module')
inside of/some/path/my_module/__init__.py
?
– James T.
Dec 7 '17 at 19:22
1
@JamesT. Yes, it's pretty common to modifysys.path
during runtime (github.com/…). You can also set PYTHONPATH environment variable.
– Igonato
Dec 8 '17 at 4:32
add a comment |
TL;DR: you can't do relative imports from the file you execute since __main__
module is not a part of a package.
Absolute imports - import something available on sys.path
Relative imports - import something relative to the current module, must be a part of a package
If you're running both variants in exactly the same way, one of them should work. Anyway, here is an example that should help you understand what's going on, let's add another main.py
file with the overall directory structure like this:
.
./main.py
./ryan/__init__.py
./ryan/config.py
./ryan/test.py
And let's update test.py to see what's going on:
# config.py
debug = True
# test.py
print(__name__)
try:
# Trying to find module in the parent package
from . import config
print(config.debug)
del config
except ImportError:
print('Relative import failed')
try:
# Trying to find module on sys.path
import config
print(config.debug)
except ModuleNotFoundError:
print('Absolute import failed')
# main.py
import ryan.test
Let's run test.py first:
$ python ryan/test.py
__main__
Relative import failed
True
Here "test" is the __main__
module and doesn't know anything about belonging to a package. However import config
should work, since the ryan
folder will be added to sys.path.
Let's run main.py instead:
$ python main.py
ryan.test
True
Absolute import failed
And here test is inside of the "ryan" package and can perform relative imports. import config
fails since implicit relative imports are not allowed in Python 3.
Hope this helped.
P.S.: if you're sticking with Python 3 there is no more need in __init__.py
files.
TL;DR: you can't do relative imports from the file you execute since __main__
module is not a part of a package.
Absolute imports - import something available on sys.path
Relative imports - import something relative to the current module, must be a part of a package
If you're running both variants in exactly the same way, one of them should work. Anyway, here is an example that should help you understand what's going on, let's add another main.py
file with the overall directory structure like this:
.
./main.py
./ryan/__init__.py
./ryan/config.py
./ryan/test.py
And let's update test.py to see what's going on:
# config.py
debug = True
# test.py
print(__name__)
try:
# Trying to find module in the parent package
from . import config
print(config.debug)
del config
except ImportError:
print('Relative import failed')
try:
# Trying to find module on sys.path
import config
print(config.debug)
except ModuleNotFoundError:
print('Absolute import failed')
# main.py
import ryan.test
Let's run test.py first:
$ python ryan/test.py
__main__
Relative import failed
True
Here "test" is the __main__
module and doesn't know anything about belonging to a package. However import config
should work, since the ryan
folder will be added to sys.path.
Let's run main.py instead:
$ python main.py
ryan.test
True
Absolute import failed
And here test is inside of the "ryan" package and can perform relative imports. import config
fails since implicit relative imports are not allowed in Python 3.
Hope this helped.
P.S.: if you're sticking with Python 3 there is no more need in __init__.py
files.
edited May 9 '17 at 4:07
answered May 9 '17 at 2:12
Igonato
5,11811847
5,11811847
2
Is there something I can do to make absolute imports always work? Like, callsys.path.append('/some/path/my_module')
inside of/some/path/my_module/__init__.py
?
– James T.
Dec 7 '17 at 19:22
1
@JamesT. Yes, it's pretty common to modifysys.path
during runtime (github.com/…). You can also set PYTHONPATH environment variable.
– Igonato
Dec 8 '17 at 4:32
add a comment |
2
Is there something I can do to make absolute imports always work? Like, callsys.path.append('/some/path/my_module')
inside of/some/path/my_module/__init__.py
?
– James T.
Dec 7 '17 at 19:22
1
@JamesT. Yes, it's pretty common to modifysys.path
during runtime (github.com/…). You can also set PYTHONPATH environment variable.
– Igonato
Dec 8 '17 at 4:32
2
2
Is there something I can do to make absolute imports always work? Like, call
sys.path.append('/some/path/my_module')
inside of /some/path/my_module/__init__.py
?– James T.
Dec 7 '17 at 19:22
Is there something I can do to make absolute imports always work? Like, call
sys.path.append('/some/path/my_module')
inside of /some/path/my_module/__init__.py
?– James T.
Dec 7 '17 at 19:22
1
1
@JamesT. Yes, it's pretty common to modify
sys.path
during runtime (github.com/…). You can also set PYTHONPATH environment variable.– Igonato
Dec 8 '17 at 4:32
@JamesT. Yes, it's pretty common to modify
sys.path
during runtime (github.com/…). You can also set PYTHONPATH environment variable.– Igonato
Dec 8 '17 at 4:32
add a comment |
I figured it out. Very frustrating, especially coming from python2.
You have to add a .
to the module, regardless of whether or not it is relative or absolute.
I created the directory setup as follows.
/main.py
--/lib
--/__init__.py
--/mody.py
--/modx.py
modx.py
def does_something():
return "I gave you this string."
mody.py
from modx import does_something
def loaded():
string = does_something()
print(string)
main.py
from lib import mody
mody.loaded()
when I execute main, this is what happens
$ python main.py
Traceback (most recent call last):
File "main.py", line 2, in <module>
from lib import mody
File "/mnt/c/Users/Austin/Dropbox/Source/Python/virtualenviron/mock/package/lib/mody.py", line 1, in <module>
from modx import does_something
ImportError: No module named 'modx'
I ran 2to3, and the core output was this
RefactoringTool: Refactored lib/mody.py
--- lib/mody.py (original)
+++ lib/mody.py (refactored)
@@ -1,4 +1,4 @@
-from modx import does_something
+from .modx import does_something
def loaded():
string = does_something()
RefactoringTool: Files that need to be modified:
RefactoringTool: lib/modx.py
RefactoringTool: lib/mody.py
I had to modify mody.py's import statement to fix it
try:
from modx import does_something
except ImportError:
from .modx import does_something
def loaded():
string = does_something()
print(string)
Then I ran main.py again and got the expected output
$ python main.py
I gave you this string.
Lastly, just to clean it up and make it portable between 2 and 3.
from __future__ import absolute_import
from .modx import does_something
add a comment |
I figured it out. Very frustrating, especially coming from python2.
You have to add a .
to the module, regardless of whether or not it is relative or absolute.
I created the directory setup as follows.
/main.py
--/lib
--/__init__.py
--/mody.py
--/modx.py
modx.py
def does_something():
return "I gave you this string."
mody.py
from modx import does_something
def loaded():
string = does_something()
print(string)
main.py
from lib import mody
mody.loaded()
when I execute main, this is what happens
$ python main.py
Traceback (most recent call last):
File "main.py", line 2, in <module>
from lib import mody
File "/mnt/c/Users/Austin/Dropbox/Source/Python/virtualenviron/mock/package/lib/mody.py", line 1, in <module>
from modx import does_something
ImportError: No module named 'modx'
I ran 2to3, and the core output was this
RefactoringTool: Refactored lib/mody.py
--- lib/mody.py (original)
+++ lib/mody.py (refactored)
@@ -1,4 +1,4 @@
-from modx import does_something
+from .modx import does_something
def loaded():
string = does_something()
RefactoringTool: Files that need to be modified:
RefactoringTool: lib/modx.py
RefactoringTool: lib/mody.py
I had to modify mody.py's import statement to fix it
try:
from modx import does_something
except ImportError:
from .modx import does_something
def loaded():
string = does_something()
print(string)
Then I ran main.py again and got the expected output
$ python main.py
I gave you this string.
Lastly, just to clean it up and make it portable between 2 and 3.
from __future__ import absolute_import
from .modx import does_something
add a comment |
I figured it out. Very frustrating, especially coming from python2.
You have to add a .
to the module, regardless of whether or not it is relative or absolute.
I created the directory setup as follows.
/main.py
--/lib
--/__init__.py
--/mody.py
--/modx.py
modx.py
def does_something():
return "I gave you this string."
mody.py
from modx import does_something
def loaded():
string = does_something()
print(string)
main.py
from lib import mody
mody.loaded()
when I execute main, this is what happens
$ python main.py
Traceback (most recent call last):
File "main.py", line 2, in <module>
from lib import mody
File "/mnt/c/Users/Austin/Dropbox/Source/Python/virtualenviron/mock/package/lib/mody.py", line 1, in <module>
from modx import does_something
ImportError: No module named 'modx'
I ran 2to3, and the core output was this
RefactoringTool: Refactored lib/mody.py
--- lib/mody.py (original)
+++ lib/mody.py (refactored)
@@ -1,4 +1,4 @@
-from modx import does_something
+from .modx import does_something
def loaded():
string = does_something()
RefactoringTool: Files that need to be modified:
RefactoringTool: lib/modx.py
RefactoringTool: lib/mody.py
I had to modify mody.py's import statement to fix it
try:
from modx import does_something
except ImportError:
from .modx import does_something
def loaded():
string = does_something()
print(string)
Then I ran main.py again and got the expected output
$ python main.py
I gave you this string.
Lastly, just to clean it up and make it portable between 2 and 3.
from __future__ import absolute_import
from .modx import does_something
I figured it out. Very frustrating, especially coming from python2.
You have to add a .
to the module, regardless of whether or not it is relative or absolute.
I created the directory setup as follows.
/main.py
--/lib
--/__init__.py
--/mody.py
--/modx.py
modx.py
def does_something():
return "I gave you this string."
mody.py
from modx import does_something
def loaded():
string = does_something()
print(string)
main.py
from lib import mody
mody.loaded()
when I execute main, this is what happens
$ python main.py
Traceback (most recent call last):
File "main.py", line 2, in <module>
from lib import mody
File "/mnt/c/Users/Austin/Dropbox/Source/Python/virtualenviron/mock/package/lib/mody.py", line 1, in <module>
from modx import does_something
ImportError: No module named 'modx'
I ran 2to3, and the core output was this
RefactoringTool: Refactored lib/mody.py
--- lib/mody.py (original)
+++ lib/mody.py (refactored)
@@ -1,4 +1,4 @@
-from modx import does_something
+from .modx import does_something
def loaded():
string = does_something()
RefactoringTool: Files that need to be modified:
RefactoringTool: lib/modx.py
RefactoringTool: lib/mody.py
I had to modify mody.py's import statement to fix it
try:
from modx import does_something
except ImportError:
from .modx import does_something
def loaded():
string = does_something()
print(string)
Then I ran main.py again and got the expected output
$ python main.py
I gave you this string.
Lastly, just to clean it up and make it portable between 2 and 3.
from __future__ import absolute_import
from .modx import does_something
edited Sep 16 '17 at 23:00
answered Aug 7 '17 at 21:32
user3159377
add a comment |
add a comment |
Tryed your example
from . import config
got the following SystemError:
/usr/bin/python3.4 test.py
Traceback (most recent call last):
File "test.py", line 1, in
from . import config
SystemError: Parent module '' not loaded, cannot perform relative import
This will work for me:
import config
print('debug=%s'%config.debug)
>>>debug=True
Tested with Python:3.4.2 - PyCharm 2016.3.2
Beside this PyCharm offers you to Import this name.
You hav to click on config
and a help icon appears.
add a comment |
Tryed your example
from . import config
got the following SystemError:
/usr/bin/python3.4 test.py
Traceback (most recent call last):
File "test.py", line 1, in
from . import config
SystemError: Parent module '' not loaded, cannot perform relative import
This will work for me:
import config
print('debug=%s'%config.debug)
>>>debug=True
Tested with Python:3.4.2 - PyCharm 2016.3.2
Beside this PyCharm offers you to Import this name.
You hav to click on config
and a help icon appears.
add a comment |
Tryed your example
from . import config
got the following SystemError:
/usr/bin/python3.4 test.py
Traceback (most recent call last):
File "test.py", line 1, in
from . import config
SystemError: Parent module '' not loaded, cannot perform relative import
This will work for me:
import config
print('debug=%s'%config.debug)
>>>debug=True
Tested with Python:3.4.2 - PyCharm 2016.3.2
Beside this PyCharm offers you to Import this name.
You hav to click on config
and a help icon appears.
Tryed your example
from . import config
got the following SystemError:
/usr/bin/python3.4 test.py
Traceback (most recent call last):
File "test.py", line 1, in
from . import config
SystemError: Parent module '' not loaded, cannot perform relative import
This will work for me:
import config
print('debug=%s'%config.debug)
>>>debug=True
Tested with Python:3.4.2 - PyCharm 2016.3.2
Beside this PyCharm offers you to Import this name.
You hav to click on config
and a help icon appears.
answered May 5 '17 at 8:35
stovfl
7,3483931
7,3483931
add a comment |
add a comment |
Setting PYTHONPATH can also help with this problem.
Here is how it can be done. Please refer the line highlighted in green
2
setting PYTHONPATH to main code directory solved the problem for me!
– Geek
May 22 at 4:21
add a comment |
Setting PYTHONPATH can also help with this problem.
Here is how it can be done. Please refer the line highlighted in green
2
setting PYTHONPATH to main code directory solved the problem for me!
– Geek
May 22 at 4:21
add a comment |
Setting PYTHONPATH can also help with this problem.
Here is how it can be done. Please refer the line highlighted in green
Setting PYTHONPATH can also help with this problem.
Here is how it can be done. Please refer the line highlighted in green
answered Feb 2 at 4:51
Santosh Pillai
2,82311813
2,82311813
2
setting PYTHONPATH to main code directory solved the problem for me!
– Geek
May 22 at 4:21
add a comment |
2
setting PYTHONPATH to main code directory solved the problem for me!
– Geek
May 22 at 4:21
2
2
setting PYTHONPATH to main code directory solved the problem for me!
– Geek
May 22 at 4:21
setting PYTHONPATH to main code directory solved the problem for me!
– Geek
May 22 at 4:21
add a comment |
This example works on Python 3.6.
I suggest going to Run -> Edit Configurations
in PyCharm, deleting any entries there, and trying to run the code through PyCharm again.
If that doesn't work, check your project interpreter (Settings -> Project Interpreter) and run configuration defaults (Run -> Edit Configurations...).
add a comment |
This example works on Python 3.6.
I suggest going to Run -> Edit Configurations
in PyCharm, deleting any entries there, and trying to run the code through PyCharm again.
If that doesn't work, check your project interpreter (Settings -> Project Interpreter) and run configuration defaults (Run -> Edit Configurations...).
add a comment |
This example works on Python 3.6.
I suggest going to Run -> Edit Configurations
in PyCharm, deleting any entries there, and trying to run the code through PyCharm again.
If that doesn't work, check your project interpreter (Settings -> Project Interpreter) and run configuration defaults (Run -> Edit Configurations...).
This example works on Python 3.6.
I suggest going to Run -> Edit Configurations
in PyCharm, deleting any entries there, and trying to run the code through PyCharm again.
If that doesn't work, check your project interpreter (Settings -> Project Interpreter) and run configuration defaults (Run -> Edit Configurations...).
answered May 11 '17 at 21:27
Carson Crane
1,101614
1,101614
add a comment |
add a comment |
As was stated in the comments to the original post, this seemed to be an issue with the python interpreter I was using for whatever reason, and not something wrong with the python scripts. I switched over from the WinPython bundle to the official python 3.6 from python.org and it worked just fine. thanks for the help everyone :)
Hmm hate to say this but same thing just happened to me. Recreating environment fix the issue. In my case, I was getting this error when running tests. In the same environment, attempt to import same module worked. Recreating environment fixed them all (same python version 3.6)
– naoko
Aug 1 '17 at 19:27
Different IDE's have different way of handling path's specially for project source files (views, modules, templates, etc.) If your project is structured and coded properly, then it should work to all (standard) IDE's. Having issues with popular IDE's like WinPython means the problem is indeed coming from your project. As mentioned above, the problem is "You have to add a . to the module" by user3159377 which should be the accepted answer.
– winux
Dec 12 at 8:41
add a comment |
As was stated in the comments to the original post, this seemed to be an issue with the python interpreter I was using for whatever reason, and not something wrong with the python scripts. I switched over from the WinPython bundle to the official python 3.6 from python.org and it worked just fine. thanks for the help everyone :)
Hmm hate to say this but same thing just happened to me. Recreating environment fix the issue. In my case, I was getting this error when running tests. In the same environment, attempt to import same module worked. Recreating environment fixed them all (same python version 3.6)
– naoko
Aug 1 '17 at 19:27
Different IDE's have different way of handling path's specially for project source files (views, modules, templates, etc.) If your project is structured and coded properly, then it should work to all (standard) IDE's. Having issues with popular IDE's like WinPython means the problem is indeed coming from your project. As mentioned above, the problem is "You have to add a . to the module" by user3159377 which should be the accepted answer.
– winux
Dec 12 at 8:41
add a comment |
As was stated in the comments to the original post, this seemed to be an issue with the python interpreter I was using for whatever reason, and not something wrong with the python scripts. I switched over from the WinPython bundle to the official python 3.6 from python.org and it worked just fine. thanks for the help everyone :)
As was stated in the comments to the original post, this seemed to be an issue with the python interpreter I was using for whatever reason, and not something wrong with the python scripts. I switched over from the WinPython bundle to the official python 3.6 from python.org and it worked just fine. thanks for the help everyone :)
answered May 12 '17 at 13:30
Ryan
1,66831723
1,66831723
Hmm hate to say this but same thing just happened to me. Recreating environment fix the issue. In my case, I was getting this error when running tests. In the same environment, attempt to import same module worked. Recreating environment fixed them all (same python version 3.6)
– naoko
Aug 1 '17 at 19:27
Different IDE's have different way of handling path's specially for project source files (views, modules, templates, etc.) If your project is structured and coded properly, then it should work to all (standard) IDE's. Having issues with popular IDE's like WinPython means the problem is indeed coming from your project. As mentioned above, the problem is "You have to add a . to the module" by user3159377 which should be the accepted answer.
– winux
Dec 12 at 8:41
add a comment |
Hmm hate to say this but same thing just happened to me. Recreating environment fix the issue. In my case, I was getting this error when running tests. In the same environment, attempt to import same module worked. Recreating environment fixed them all (same python version 3.6)
– naoko
Aug 1 '17 at 19:27
Different IDE's have different way of handling path's specially for project source files (views, modules, templates, etc.) If your project is structured and coded properly, then it should work to all (standard) IDE's. Having issues with popular IDE's like WinPython means the problem is indeed coming from your project. As mentioned above, the problem is "You have to add a . to the module" by user3159377 which should be the accepted answer.
– winux
Dec 12 at 8:41
Hmm hate to say this but same thing just happened to me. Recreating environment fix the issue. In my case, I was getting this error when running tests. In the same environment, attempt to import same module worked. Recreating environment fixed them all (same python version 3.6)
– naoko
Aug 1 '17 at 19:27
Hmm hate to say this but same thing just happened to me. Recreating environment fix the issue. In my case, I was getting this error when running tests. In the same environment, attempt to import same module worked. Recreating environment fixed them all (same python version 3.6)
– naoko
Aug 1 '17 at 19:27
Different IDE's have different way of handling path's specially for project source files (views, modules, templates, etc.) If your project is structured and coded properly, then it should work to all (standard) IDE's. Having issues with popular IDE's like WinPython means the problem is indeed coming from your project. As mentioned above, the problem is "You have to add a . to the module" by user3159377 which should be the accepted answer.
– winux
Dec 12 at 8:41
Different IDE's have different way of handling path's specially for project source files (views, modules, templates, etc.) If your project is structured and coded properly, then it should work to all (standard) IDE's. Having issues with popular IDE's like WinPython means the problem is indeed coming from your project. As mentioned above, the problem is "You have to add a . to the module" by user3159377 which should be the accepted answer.
– winux
Dec 12 at 8:41
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.
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%2f43728431%2frelative-imports-modulenotfounderror-no-module-named-x%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
I cannot reproduce the error, how do you execute this code?
– Copperfield
May 5 '17 at 1:57
I execute
test.py
through pyCharm with Python 3.6. Does yours execute fine?– Ryan
May 5 '17 at 3:18
1
I execute it with idle that come with python, and also as
python test.py
, and it work perfectly fine. I don't have pyCharm, but perhaps is some bad configuration of pyCharm that is causing the problem– Copperfield
May 5 '17 at 4:03
1
Very odd. I'm using WinPython - just download vanilla Python 3.6 from python.org, and it works fine. Never thought to check the interpreter! Thanks!
– Ryan
May 5 '17 at 5:17
1
My guess is that something funky is going on with PYTHONPATH. Check your IDE settings and/or system environment variables.
– Martin Tournoij
May 6 '17 at 19:53