简体   繁体   中英

Import python file from another folder that is not a child

What I want

So I'm using Visual Studio code and Python 3.7.0 and I'm just trying to import another python file, from another folder, into my python file .


Details

Here is my folder structure

root/
    dir1/
        data.txt
        task11.py
        task12.py
    dir2/
        data.txt
        task21.py
        task22.py
    Helpers/
        FileHelper/
            ReadHelper.py

So a short explanation:

  • I use the same function in every "task"-file
  • Instead of putting the function in every "task"-file, I've created a helper file where the function exists
  • I want to import the helper file "ReadHelper.py" into my task files

What I've tried

eg in the file task11.py:

  • from Helpers.FileHelper.ReadHelper import *
  •  import os, sys parentPath = os.path.abspath("../../") if parentPath not in sys.path: sys.path.insert(0, parentPath) from Helpers.FileHelper.ReadHelper import *
  •  import os, sys sys.path.append('../../') from Helpers.FileHelper.ReadHelper import *

None of the above solutions works as I always end up with the error: ModuleNotFoundError: No module named 'Helpers'

I've also tried:

  • from ..Helpers.FileHelper.ReadHelper import *

But it ends up with the error: ValueError: attempted relative import beyond top-level package

So how can I import the file ReadHelper.py to my task files?


PS

There are some similar questions to this but they are really old and the answers have not helped me.


Update 1

There is an option in Visual Studio code, vscode python 命令 If I run this command with this import from Helpers.FileHelper import ReadHelper then no errors are generated and the code executes perfectly.

One downside is that this interactive window is slow at starting and it cannot handle inputs.

I tried the answer of @Omni as well:

$> python -m root.dir1.task11

And it worked! but as he said there is a downside, which is that it is slow to type in the terminal.

So I tried to create a task in Visual Studio Code that could execute the above shell command for the file that I'm currently is in, but did not succeed.

Do you know how to crate a task in vscode to run the above command?


I've also tried to add __init__.py -files under every directory so they would be seen as packages Python3 tutorial - 6.4 Module Packages . But this didn't help and the same error occurred.


Update 2

I come up with a way to make it really easy to have a folder structure like this and get the imports to work correctly in the terminal.

Basically what I did was:

  • created a pyhton script
  • created a task in visual studio code

With this I can now run my python files, with the imports, by only pressing cmd + shift + B .


Explanation

The visual studio task:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run python file",
            "type": "shell",
            "command": "python3 /PATH_TO_ROOT_FOLDER/run_python_file.py ${file}",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "new",
                "focus": true
            }
        }
    ]
}

The part that we want to focus on is this one:

"command": "python3 /PATH_TO_ROOT_FOLDER/run_python_file.py ${file}",

  • This part run the new python file I created at the root folder, and passes the path, of the file which is active, as a parameter

The python script:

import os, sys

# This is a argument given trough a shell command
PATH_TO_MODULE_TO_RUN = sys.argv[1]

ROOT_FOLDER = "root/"

def run_module_gotten_from_shell():
    # Here I take only the part of the path that is needed
    relative_path_to_file = PATH_TO_MODULE_TO_RUN.split(ROOT_FOLDER)[1]

    # Creating the shell command I want to run
    shell_command = createShellCommand(relative_path_to_file)

    os.system(shell_command)


# Returning "python3 -m PATH.TO.MODULE"
def createShellCommand(relative_path_to_file):
    part1 = "python3"
    part2 = "-m"

    # Here I change the string "dir1/task11.py" => "dir1.task11"
    part3 = relative_path_to_file.replace("/", ".")[:-3]

    shell_command = "{:s} {:s} {:s}".format(part1, part2, part3)
    return shell_command

run_module_gotten_from_shell()
  • This python script gets as parameter the path to the active file
  • Then it creates a shell command of the path (the shell command is like @kasper-keinänen 's answer)
  • Then it run that shell command

With these modifications, I can run any file inside the root directory with imports from any file inside the root directory.

And I can do it by only pressing cmd + shift + B .

You could try running the script with the -m option that allows modules to be located using the Python module namespace docs.python.org .

If you run the task11.py script then:

$ python3 -m dir1.task11 

And in the task11.py do the import like:

from Helpers.FileHelper.ReadHelper import *

If you're only trying to do this in VSCode, and not during normal run time. You can add the path in the .vscode/settings.json

{
    "python.analysis.extraPaths": [
        "${workspaceFolder}/webapp"
    ],
}

在此处输入图片说明

NOTE: This does not solve standard Python importing. My use-case was specific to a monolithic project where all editor config files where in the root and thus I couldn't open 'webapp/' as a workspace itself.

Adding the full absolute path to the sys.path variable should make it work.

import sys
sys.path.append('/full/path/to/Helpers/FilesHelper/')

from ReadHelper import *

a) Execute the task modules as scripts within an environment that knows about the helper functions. That way the code in the taks modules does not have to know anything about the package structure present. It imitates the builtins of the python interpreter.

   # cli argument #1 is the task module to execute
   import sys  
   task_to_execute = sys.argv[1]

   from Helpers.FileHelper.ReadHelper import *
   exec(open(task_to_execute).read())

b) Use relative imports correctly. In order to do so, you have to execute the task code via (this might be a disadvantage of this solution).

   $> python -m root.dir1.task11.task11

The problem is your file/folder structure. I would suggest creating a sort of 'control' file in your root folder, which can then work from the top-down to reference all your other modules.

So let's say you had a file in your root folder called MasterTask.py it could look like this:

from dir1.task11.task11 import *
from dir1.task12.task12 import *
from dir2.task21.task21 import *
from dir2.task22.task22 import *
from Helpers.FileHelper.ReadHelper import *

class Master:
#Do your task work here
    pass

One other option would be to move the Helpers folder into your Python37\\Lib\\site-packages folder, which would also allow the use of from Helpers.FileHelper.ReadHelper import * as is - assuming that you are not planning on this to be used on other machines other than your own.

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