-5

I AM NOT TRYING TO IMPORT FROM PARENT FOLDER BUT FROM 2 DIRECTOTIES UP!!

Im am trying to import a file into an script from a relative path. Since Python can only go up 1 directory and not more I need to find the path The code I use is,

from pathlib import Path
import os
path = Path(__file__).parents[2]

p = (os.path.join(path, "Defaults\\"))

from p.Defaults1 import *

I am getting an error ":ModuleNotFoundError: No module named 'p'" when running "from p.Defaults1 import *"

p is the path and not a module. Defaults1 is the file I would like to import.

How can i solve this?

My folder structure is

MAIN FOLDER
  sub folder 1
     sub folder 2
       file_that_has_code_to_import "File_to_import"

File_to_import

As you can see I need to go back UP 2 directories in order to get the file I need

  • 5
    That's not how you import files.. what are you actually trying to do? is 'Defaults1' a python module, you're trying to import at runtime? – JeffUK Dec 30 '21 at 14:36
  • 2
    Does this answer your question? [Importing modules from parent folder](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) There are a number of proposed solutions here – JeffUK Dec 30 '21 at 14:38
  • You are setting p to the string path and appending defaults to it, but then you are incorrectly trying import a library using that (doing p. something is trying to get a property or a namespace, you cannot perform that on a string var). Importing libraries is only so you can use functions/classes like numpy which helps with building matrix's and arrays. – Daniel me Dec 30 '21 at 14:40
  • The script I need to import is 2 directories UP from the directory where the main script is. – xoxece7794 Dec 30 '21 at 14:48
  • Can't you just structure your file so it works using the standard functionality? This way your IDE will give you type checking and auto complete etc. etc. – JeffUK Dec 30 '21 at 14:58
  • Is that script supposed to be part of your project? Are you always expecting it to be in that relative location? If so, then that is what *relative imports* are for. Otherwise, if you are trying to figure out at runtime where the file to import is (for example, because you are making some kind of plugin system, and the user tells you where to look for the plugin script), then that is *dynamic import* and is much more complex. – Karl Knechtel Dec 30 '21 at 14:59
  • @KarlKnechtel I am making an addon for some program The addon has multiple functions written in multiple files. Most of the function share the same modules found in the file I try to import. Since the function I am working on now is in a sub sub folder I need to go back up 2 directories. – xoxece7794 Dec 30 '21 at 15:03

1 Answers1

0

this is the correct way to import files: importlib was added to Python 3 to programmatically import a module.

import importlib

moduleName = input('Enter module name:')
importlib.import_module(moduleName)

The .py extension should be removed from moduleName. The function also defines a package argument for relative imports.

Tal Folkman
  • 2,368
  • 1
  • 7
  • 21