Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ModuleNotFoundError when importing python files (utility.py) located in the same directory as main.py script #28

Closed
dirkteucher opened this issue Apr 30, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@dirkteucher
Copy link

dirkteucher commented Apr 30, 2024

I cannot import classes or def functions like this
from myOtherFile import utilityABC

I was wondering what the best solution is to import my own custom python files from the same directory or even sub directory with this extension?

I can do this just fine outside of vscode and the stub system everything works great but the interpreter in vscode along with the Unreal remote execution does not seem to handle importing local files in the same way.

Hopefully you have a solution as this will make my code a lot more readable.

@dirkteucher
Copy link
Author

dirkteucher commented Apr 30, 2024

I should add that there is an "absolute path" solution to this which is :

import sys
sys.path.append(r'E:\thepath\tothefolder')
from myOtherFile import utilityABC

But I was hoping there was a way to get relative paths somehow as using absolute paths is not as readable or portable.

@nils-soderman
Copy link
Owner

I was contemplating whether to add the workspace directory to sys.path but decided against modifying the user's paths.
Could potentially be added as an option in the extension though.

Otherwise to add the relative path to sys.path you can use the __file__ variable:

import sys
import os

currentDirectory = os.path.dirname(__file__)
if currentDirectory not in sys.path:
    sys.path.append(currentDirectory)

@dirkteucher
Copy link
Author

That solution works perfectly well to me. Thank you for your help with this.
If anyone else gets this problem I also noticed along with nils code above I also needed to delete the files in the pycache to refresh file changes I made while testing this out with my files. Not sure what the mechanism is to refresh changes to the files yet I need to read up on how that works more.

@nils-soderman
Copy link
Owner

You can use importlib for reloading modules:

from importlib import reload

import myOtherFile
reload(myOtherFile)

myOtherFile.utilityABC()

Thinking about this more, it'd make sense to add an option to add the workspace directory to sys.path. Considering (as you mentioned) VS Code resolves the paths for auto-completion, so when executing the code you would expect the paths to be resolved in engine as well. Will look into adding this feature!

@nils-soderman nils-soderman added the enhancement New feature or request label May 1, 2024
@dirkteucher
Copy link
Author

You pre-empted the comment I was just about to make :D deleting the pycache file each time does not work unless you rename the file but then if you try to rename the file back to the original it still does not work because the temp filename is a different timestamp so never gets checked.
But your reload code does work nicely. Thank you again., this is a pretty nice workflow. Now I can get back to making a conversion script for blender materials.

@nils-soderman
Copy link
Owner

Hi,

Just released 1.4.0, and the workspace folder(s) are now added to the Python path by default.
(Can be disabled with ue-python.environment.addWorkspaceToPath: false)

I've also added support for relative imports, which is most likely what you'd like to use if you're writing a package,
as you generally don't want your package to modify the sys.path.

Assuming you have a structure like:

my_package/
    converter.py
    utils.py
.gitignore

In converter.py you can now do:

from . import utils

*relative imports only work if they are placed in a subfolder, and cannot be in the root directory (the location defined in sys.path)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants