简体   繁体   中英

Python calling a file from another directory

I've been trying to find the answer to this but everything i look at is reading a file from another directory or running a file from another directory. I'm sure what i want to do isn't that hard but i am new at this and don't know how to describe what it's called.

I have a python script run.py that is in the /src directory. I run that script form the /src directory. run.py calls two files (configuration.py and server.py). These two files are in a folder called lib (src/lib).All folders have an empty __init__.py file.

When i take these files out of lib and put them just in src i can run the script when the script looks like it does below.

import os
import inspect
import sys


import configuration
import server


# Initialize Parameters
f_path = os.path.abspath(inspect.getfile(inspect.currentframe()))
absolute_path = os.path.dirname(f_path)


if __name__ == "__main__":
    from optparse import OptionParser, OptionGroup

    parser = OptionParser()

    parser.usage = "usage: %prog [options] "

    parser.description = "Primary application entry point."

    parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
                  default=False, help="Run verbose.")

    group = OptionGroup(parser, "Node operations")

    group.add_option("--do-queue-job", dest="do_queue_job", action="store_true",
                 help="Run the next job in the quasar-server queue.")

    parser.add_option_group(group)

    (options, args) = parser.parse_args()

    # Clear argv to prevent issues with web.py automatically using arguments to
    # bind ip addresses.
    sys.argv = []

    configuration = configuration.Configuration("/home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/config.yml")


    if (options.do_queue_job):
        # Get server instance
        server_connection = server.QuasarConnection(configuration)
        #return server_connection

        # Get next job from server
        next_job = server_connection.get_next_job()
        #return next_job

The two parts of the code i know i have to change if i move the two files to /src/lib are the following:

    configuration = configuration.Configuration("/home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/config.yml")

    server_connection = server.QuasarConnection(configuration)

i am thinking that i would just have to put.lib before them like so, but every time i try it it says lib is not defined.

    configuration = lib.configuration.Configuration("/home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/config.yml")

    server_connection = lib.server.QuasarConnection(configuration)

This is probably a noob question, but does anyone know how to target these files if the are in the src/lib directory as opposed to just the src directory

You just need to change your import statement to reflect the module's new location:

import lib.configuration as configuration
import lib.server as server

And the rest of your script doesn't really need to change.

I got it. I think your answer may have worked in certain cases but i think my problem being new at this is figuring out what to search.

it was a sys.arg thing so i had to include that path to that lib folder before i imported the files.

sys.path.insert(0, '/home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib')


import configuration
import server

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