Different module import in Mac python vs. spyder
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have recently asked this question about importing an arbitrary amount of modules in python. I received two good answers. Both worked when I programmed it in spyder.
Today I ran the script from my terminal as test, since I'm planning to move my code to my server. But this time the script crashed with this Traceback:
File "evaluation.py", line 27, in __init__
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
ImportError: No module named 'v00'
The file architecture looks like this:
-evaluation.py
-evaluation
-v00.py
-v01.py
The code in evaluation.py which causes trouble is this one:
os.chdir('evaluation')
for file_name in glob.glob("*.py"):
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
for idx, solver in enumerate(self.solvers):
self.dqn.append(solver.DQNSolver() )
Why does this work in spyder but not in the terminal? They both use python 3.5 and I double checked that both are in the folder "evaluation" when executing the malicious line.
python macos module spyder
add a comment |
I have recently asked this question about importing an arbitrary amount of modules in python. I received two good answers. Both worked when I programmed it in spyder.
Today I ran the script from my terminal as test, since I'm planning to move my code to my server. But this time the script crashed with this Traceback:
File "evaluation.py", line 27, in __init__
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
ImportError: No module named 'v00'
The file architecture looks like this:
-evaluation.py
-evaluation
-v00.py
-v01.py
The code in evaluation.py which causes trouble is this one:
os.chdir('evaluation')
for file_name in glob.glob("*.py"):
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
for idx, solver in enumerate(self.solvers):
self.dqn.append(solver.DQNSolver() )
Why does this work in spyder but not in the terminal? They both use python 3.5 and I double checked that both are in the folder "evaluation" when executing the malicious line.
python macos module spyder
What does your import statement look like?
– l'L'l
Jan 3 at 22:53
@l'L'l only os and glob are imported
– Mr.Sh4nnon
Jan 4 at 8:17
add a comment |
I have recently asked this question about importing an arbitrary amount of modules in python. I received two good answers. Both worked when I programmed it in spyder.
Today I ran the script from my terminal as test, since I'm planning to move my code to my server. But this time the script crashed with this Traceback:
File "evaluation.py", line 27, in __init__
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
ImportError: No module named 'v00'
The file architecture looks like this:
-evaluation.py
-evaluation
-v00.py
-v01.py
The code in evaluation.py which causes trouble is this one:
os.chdir('evaluation')
for file_name in glob.glob("*.py"):
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
for idx, solver in enumerate(self.solvers):
self.dqn.append(solver.DQNSolver() )
Why does this work in spyder but not in the terminal? They both use python 3.5 and I double checked that both are in the folder "evaluation" when executing the malicious line.
python macos module spyder
I have recently asked this question about importing an arbitrary amount of modules in python. I received two good answers. Both worked when I programmed it in spyder.
Today I ran the script from my terminal as test, since I'm planning to move my code to my server. But this time the script crashed with this Traceback:
File "evaluation.py", line 27, in __init__
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
ImportError: No module named 'v00'
The file architecture looks like this:
-evaluation.py
-evaluation
-v00.py
-v01.py
The code in evaluation.py which causes trouble is this one:
os.chdir('evaluation')
for file_name in glob.glob("*.py"):
self.solvers.append( __import__(file_name[:-3]) ) #cut away .py
for idx, solver in enumerate(self.solvers):
self.dqn.append(solver.DQNSolver() )
Why does this work in spyder but not in the terminal? They both use python 3.5 and I double checked that both are in the folder "evaluation" when executing the malicious line.
python macos module spyder
python macos module spyder
asked Jan 3 at 22:39
Mr.Sh4nnonMr.Sh4nnon
7111
7111
What does your import statement look like?
– l'L'l
Jan 3 at 22:53
@l'L'l only os and glob are imported
– Mr.Sh4nnon
Jan 4 at 8:17
add a comment |
What does your import statement look like?
– l'L'l
Jan 3 at 22:53
@l'L'l only os and glob are imported
– Mr.Sh4nnon
Jan 4 at 8:17
What does your import statement look like?
– l'L'l
Jan 3 at 22:53
What does your import statement look like?
– l'L'l
Jan 3 at 22:53
@l'L'l only os and glob are imported
– Mr.Sh4nnon
Jan 4 at 8:17
@l'L'l only os and glob are imported
– Mr.Sh4nnon
Jan 4 at 8:17
add a comment |
1 Answer
1
active
oldest
votes
The typical way to handle this would be to turn the folder into a package by adding an empty __init__.py
file and then import from the package with import evaluation.v00
(or the equivalent __import__
function call). But you may run into problems as your main script has the same name as the package. I would suggest renaming one or the other
-evaluationscript.py
-evaluation
-__init__.py (empty file)
-v00.py
-v01.py
And then you probably need to use import_module instead of __import__
to populate solvers
with the actual module (instead of the package).
I'm not familiar with spyder, but if the same code is working there, then it may be adding the evaluation folder to the search path either with the PYTHONPATH
env var, or by modifying sys.path
.
When you run a script, the path of the script is added to the default search path for module imports, but changing the folder using os.chdir
won't affect that search path.
thanks! spyder added probably something to the PYTHONPATH. Accessing the module by foldername.module worked.
– Mr.Sh4nnon
Jan 4 at 9:59
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%2f54030784%2fdifferent-module-import-in-mac-python-vs-spyder%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
The typical way to handle this would be to turn the folder into a package by adding an empty __init__.py
file and then import from the package with import evaluation.v00
(or the equivalent __import__
function call). But you may run into problems as your main script has the same name as the package. I would suggest renaming one or the other
-evaluationscript.py
-evaluation
-__init__.py (empty file)
-v00.py
-v01.py
And then you probably need to use import_module instead of __import__
to populate solvers
with the actual module (instead of the package).
I'm not familiar with spyder, but if the same code is working there, then it may be adding the evaluation folder to the search path either with the PYTHONPATH
env var, or by modifying sys.path
.
When you run a script, the path of the script is added to the default search path for module imports, but changing the folder using os.chdir
won't affect that search path.
thanks! spyder added probably something to the PYTHONPATH. Accessing the module by foldername.module worked.
– Mr.Sh4nnon
Jan 4 at 9:59
add a comment |
The typical way to handle this would be to turn the folder into a package by adding an empty __init__.py
file and then import from the package with import evaluation.v00
(or the equivalent __import__
function call). But you may run into problems as your main script has the same name as the package. I would suggest renaming one or the other
-evaluationscript.py
-evaluation
-__init__.py (empty file)
-v00.py
-v01.py
And then you probably need to use import_module instead of __import__
to populate solvers
with the actual module (instead of the package).
I'm not familiar with spyder, but if the same code is working there, then it may be adding the evaluation folder to the search path either with the PYTHONPATH
env var, or by modifying sys.path
.
When you run a script, the path of the script is added to the default search path for module imports, but changing the folder using os.chdir
won't affect that search path.
thanks! spyder added probably something to the PYTHONPATH. Accessing the module by foldername.module worked.
– Mr.Sh4nnon
Jan 4 at 9:59
add a comment |
The typical way to handle this would be to turn the folder into a package by adding an empty __init__.py
file and then import from the package with import evaluation.v00
(or the equivalent __import__
function call). But you may run into problems as your main script has the same name as the package. I would suggest renaming one or the other
-evaluationscript.py
-evaluation
-__init__.py (empty file)
-v00.py
-v01.py
And then you probably need to use import_module instead of __import__
to populate solvers
with the actual module (instead of the package).
I'm not familiar with spyder, but if the same code is working there, then it may be adding the evaluation folder to the search path either with the PYTHONPATH
env var, or by modifying sys.path
.
When you run a script, the path of the script is added to the default search path for module imports, but changing the folder using os.chdir
won't affect that search path.
The typical way to handle this would be to turn the folder into a package by adding an empty __init__.py
file and then import from the package with import evaluation.v00
(or the equivalent __import__
function call). But you may run into problems as your main script has the same name as the package. I would suggest renaming one or the other
-evaluationscript.py
-evaluation
-__init__.py (empty file)
-v00.py
-v01.py
And then you probably need to use import_module instead of __import__
to populate solvers
with the actual module (instead of the package).
I'm not familiar with spyder, but if the same code is working there, then it may be adding the evaluation folder to the search path either with the PYTHONPATH
env var, or by modifying sys.path
.
When you run a script, the path of the script is added to the default search path for module imports, but changing the folder using os.chdir
won't affect that search path.
answered Jan 3 at 23:17
Peter GibsonPeter Gibson
13.5k34347
13.5k34347
thanks! spyder added probably something to the PYTHONPATH. Accessing the module by foldername.module worked.
– Mr.Sh4nnon
Jan 4 at 9:59
add a comment |
thanks! spyder added probably something to the PYTHONPATH. Accessing the module by foldername.module worked.
– Mr.Sh4nnon
Jan 4 at 9:59
thanks! spyder added probably something to the PYTHONPATH. Accessing the module by foldername.module worked.
– Mr.Sh4nnon
Jan 4 at 9:59
thanks! spyder added probably something to the PYTHONPATH. Accessing the module by foldername.module worked.
– Mr.Sh4nnon
Jan 4 at 9:59
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%2f54030784%2fdifferent-module-import-in-mac-python-vs-spyder%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
What does your import statement look like?
– l'L'l
Jan 3 at 22:53
@l'L'l only os and glob are imported
– Mr.Sh4nnon
Jan 4 at 8:17