MadnessRed
July 7th, 2010, 04:50 PM
Here is my code:
#Basic
import os
#Current cwd
cwd = os.getcwd()
#Move in to folder
newcwd = '/home/madnessred/Python/FM/folder_views'
#newcwd = os.path.join(os.path.split(repr(__file__))[0][1:], 'folder_views')
#Set as cwd
os.chdir(newcwd)
#Dictionary to hold folderviews
folder_views = {}
#For each file
for view in os.listdir(os.getcwd()):
#Is it as expected
if view.split('.')[1] == 'py' and len(view.split('.')) == 2:
#Import it
folder_views[view.split('.')[0]] = __import__(view.split('.')[0], globals(), locals(), ['view'])
#Go back to original dir
os.chdir(cwd)
#Output
print folder_views
If I run that file, I get:
madnessred@Anthony-PC:~$ python ./Python/FM/folder.py
Traceback (most recent call last):
File "./Python/FM/folder.py", line 24, in <module>
folder_views[view.split('.')[0]] = __import__(view.split('.')[0], globals(), locals(), ['view'])
ImportError: No module named icons
Whereas, if I do, exactly the same thing in the ide.
madnessred@Anthony-PC:~$ python
Python 2.6.5 (r265:79063, Mar 20 2010, 14:41:07)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> cwd = os.getcwd()
>>> newcwd = '/home/madnessred/Python/FM/folder_views'
>>> os.chdir(newcwd)
>>> folder_views = {}
>>> for view in os.listdir(os.getcwd()):
... if view.split('.')[1] == 'py' and len(view.split('.')) == 2:
... folder_views[view.split('.')[0]] = __import__(view.split('.')[0], globals(), locals(), ['view'])
...
>>> os.chdir(cwd)
>>> print folder_views
{'icons': <module 'icons' from 'icons.pyc'>}
>>>
It works fine in the ide, (the terminal one), but when i try and run the .py file it comes up with that error.
I have tried renaming the modules, I have checked os.getcwd() where it is importing and that is right.
Could someone please show me where I have gone wrong, or suggest a better way of doing it.
MadnessRed
#Basic
import os
#Current cwd
cwd = os.getcwd()
#Move in to folder
newcwd = '/home/madnessred/Python/FM/folder_views'
#newcwd = os.path.join(os.path.split(repr(__file__))[0][1:], 'folder_views')
#Set as cwd
os.chdir(newcwd)
#Dictionary to hold folderviews
folder_views = {}
#For each file
for view in os.listdir(os.getcwd()):
#Is it as expected
if view.split('.')[1] == 'py' and len(view.split('.')) == 2:
#Import it
folder_views[view.split('.')[0]] = __import__(view.split('.')[0], globals(), locals(), ['view'])
#Go back to original dir
os.chdir(cwd)
#Output
print folder_views
If I run that file, I get:
madnessred@Anthony-PC:~$ python ./Python/FM/folder.py
Traceback (most recent call last):
File "./Python/FM/folder.py", line 24, in <module>
folder_views[view.split('.')[0]] = __import__(view.split('.')[0], globals(), locals(), ['view'])
ImportError: No module named icons
Whereas, if I do, exactly the same thing in the ide.
madnessred@Anthony-PC:~$ python
Python 2.6.5 (r265:79063, Mar 20 2010, 14:41:07)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> cwd = os.getcwd()
>>> newcwd = '/home/madnessred/Python/FM/folder_views'
>>> os.chdir(newcwd)
>>> folder_views = {}
>>> for view in os.listdir(os.getcwd()):
... if view.split('.')[1] == 'py' and len(view.split('.')) == 2:
... folder_views[view.split('.')[0]] = __import__(view.split('.')[0], globals(), locals(), ['view'])
...
>>> os.chdir(cwd)
>>> print folder_views
{'icons': <module 'icons' from 'icons.pyc'>}
>>>
It works fine in the ide, (the terminal one), but when i try and run the .py file it comes up with that error.
I have tried renaming the modules, I have checked os.getcwd() where it is importing and that is right.
Could someone please show me where I have gone wrong, or suggest a better way of doing it.
MadnessRed