Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing libraries
#1
I am trying to import libraries with .py extension by creating the following:
import osfiles = os.popen('dir *.py')fileit = iter(files)for file in files:        print(file) 
From this i get the following:
 sh: dir: command not found

Any help would be great, cheers.

Sandy
Reply
#2
You need to post your code, output and errors between their respective tags. Refer to the Help Document on how to properly post a question. Otherwise people have to guess what you're trying to do, or not respond at all.

I am guessing that by this:
import osfiles = os.popen('dir *.py')fileit = iter(files)for file in files:        print(file) 
you actually mean this:
import os

files = os.popen('dir *.py')
fileit = iter(files)
for file in files:
    print(file) 
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
You should use the module glob for this task: https://docs.python.org/3/library/glob.html
Or just use pathlib: https://docs.python.org/3/library/pathlib.html
And for importing, importlib: https://docs.python.org/3/library/importlib.html


I guess you want to have something like a plugin directory and your script should load them dynamically.
But this is only the half work. Somewhere you've to register this loaded plugins, to announce them in your
main program.


import glob
import importlib
import pathlib

# without pathlib
# using module glob
plugindirectory = 'plugins'
loaded_plugin_modules = []
for file in glob.glob1(plugindirectory, '*.py'):
    stem, suffix = file.rsplit('.', 1)
    to_import = 'plugins.{}'.format(stem)
    print('Importing:', to_import)
    module = importlib.import_module(to_import)
    loaded_plugin_modules.append(module)

print(loaded_plugin_modules)


# with pathlib
print('Using pathlib.Path')
plugindirectory = pathlib.Path('plugins')
loaded_plugin_modules = []
for file in plugindirectory.glob('*.py'):
    to_import = 'plugins.{}'.format(file.stem)
    print('Importing:', to_import)
    module = importlib.import_module(to_import)
    loaded_plugin_modules.append(module)

print(loaded_plugin_modules)

Instead of using stem, suffix = file.rsplit('.', 1), you can use also stem, suffix = os.path.splitext(file)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
(Jul-26-2017, 11:23 AM)u18sc11 Wrote: From this i get the following:
 sh: dir: command not found

dir is a windows thing. sh is not a windows thing. If you're mixing environments, then you should use libraries that are made to be platform independent, like the glob module (import glob, then glob.glob("*.py")).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a Tensor without importing any modules or libraries samW 1 1,901 Sep-21-2020, 06:29 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020