0

I have a string variable x, and want to use input to call a module in a subfolder, as shown below. How can I use this string as part of the path?

x = input()

from subfolder.x import y

My code is run from a parent folder 'main.py' and uses the line:

os.chdir(os.path.dirname(os.path.abspath(__file__)))

to set the file path.

2
  • The method in your other module could accept a parameter for the path? It's not really clear what you're trying to do. Perhaps extend your example including inputs and desired output(s). Commented Mar 2, 2022 at 11:29
  • In this example, I am only trying to demonstrate importing module x. This is the relevant bit, as the rest of my code is structured to accommodate it, but I cannot get this line to run without an error. Commented Mar 2, 2022 at 12:13

2 Answers 2

1

If you need to dynamically import, take look at importlib.import_module consider following simple example

import importlib
modulename = "html"
submodulename = "entities"
what = "html5"
module = importlib.import_module(modulename + "." + submodulename)
thing = getattr(module,what)
print(thing["gt;"])  # >
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for mentioning this - when I've tried it previously, I've got an error saying the package doesn't exit. Does this module only work with packages? As I am only trying to import another module in the form of a .py file.
@PublicMutiny you can use if you have .py file, remembering to NOT include *.py, for example if you have config.py then you might do config = importlib.import_module("config")
0

According to Python documentation, here is how an import statement searches for the correct module or package to import:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.pathsys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory.

Source: Python 2 and 3

So you can use sys.path.append to append a location to the path, but once you close Python, sys.path will be set back to default.
Or you can add the desired location permannently to PYTHONPATH by adding

export PYTHONPATH="${PYTHONPATH}:/my/other/path"

in a startup script appropriate to whatever shell you're using (.bashrc for bash, for example)

1 Comment

Thanks for sharing this - how would I use this to find modules using a string variable name? As that is the problem I'm currently struggling with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.