简体   繁体   中英

Different module import in Mac python vs. 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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM