简体   繁体   中英

Python3.6 |- Import a module from a list with __import__ -|

I'm creating a really basic program that simulates a terminal with Python3.6, its name is Prosser(The origin is that "Prosser" sounds like "Processer" and Prosser is a command processer).

A problem that I'm having is with command import, this is, all the commands are stored in a folder called " lib " in the root folder of Prosser and inside it can have folders and files, if a folder is in the " lib " dir it can't be named as folder anymore, its name now is package (But this doesn't care for now).

The interface of the program is just a input writed:

Prosser:/home/pedro/documents/projects/prosser/-!

and the user can type a command before the text, like a normal terminal:

Prosser:/home/pedro/documents/projects/prosser/-! console.write Hello World

let's say that inside the " lib " folder exists one folder called " console " and inside it has a file called " write.py " that has the code:

class exec:
    def main(args):
        print(args)

As you can see the first 2 lines is like a important structure for command execution: The class " exec " is the main class for the command execution and the def " main " is the main and first function that the terminal will read and execute also pass the arguments that the user defined, after that the command will be responsible to catch any error and do what it will be created to do.

At this moment, everything is ok, but now comes the true help that I need of U guys, the command import.

Like I writed the user can type any command, and in the example above I typed a " console.write Hello World " and exists one folder called " console " and one file " write.py ". The point is that the packages can be defined by a "dot", this is:

-! write Hello World

Above I only typed " write " and this says that the file is only inside the " lib " folder, it doesn't has a package to storage and separate it, so it is a Freedom command (A command that doesn't has packages or nodes).

-! console.write Hello World

Now I typed above " console.write " and this says that the file has a package or node to storage and separate it, this means that it is a Tied command (A command that has packages or nodes).

With that, a file is separated from the package(s) with a dot, the more dots you put, more folders will be navigated to find the file and proceed to the next execution.

Code

Finnaly the code. With the import statement I tryied this:

import os
import form

curDir = os.path.dirname(os.path.abspath(__file__)) # Returns the current direrctory open

while __name__ == "__main__":
    c = input('Prosser:%s-! ' % (os.getcwd())).split() # Think the user typed "task.kill"
    path = c[0].split('.') # Returns a list like: ["task", "kill"]
    try:
        args = c[1:] # Try get the command arguments
        format = form.formater(args) # Just a text formatation like create a new line with "$n"
    except:
        args = None # If no arguments the args will just turn the None var type
        pass 

     if os.path.exists(curDir + "/lib/" + "/".join(path) + ".py"): # If curDir+/lib/task/kill.py exists
         module = __import__("lib." + '.'.join(path)) # Returns "lib.task.kill"
         module.exec.main(args) # Execute the "exec" class and the def "**main**"
     else:
         pathlast = path[-1] # Get the last item, in this case "kill"
         path.remove(path[-1]) # Remove the last item, "kill"
         print('Error: Unknow command: ' + '.'.join(path) + '. >>' + pathlast + '<<') # Returns an error message like: "Error: Unknow command: task. >>kill<<"
    # Reset the input interface

The problem is that when the line "module = __import__("lib." + '.'.join(path))" is executed the console prints the error:

Traceback (most recent call last):
    File "/home/pedro/documents/projects/prosser/main.py", line 18, in <module>
    module.exec.main(path) # Execute the "exec" class and the def "**main**"
AttributeError: module 'lib' has no attribute 'exec'

I also tried to use:

module = \_\_import\_\_(curDir + "lib." + '.'.join(path))

But it gets the same error. I think it's lighter for now. I'd like if someone help me or find some replacement of the code. :)

I think you have error here:

You have diffirent path here:

if os.path.exists(curDir + "/lib/" + "/".join(path) + ".py")

And another here, you dont have curDir:

module = __import__("lib." + '.'.join(path)) # Returns "lib.task.kill"

You should use os.path.join to build paths like this:

module = __import__(os.path.join(curdir, 'lib', path + '.py'))

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