vscode-unreal-python
vscode-unreal-python copied to clipboard
ModuleNotFoundError when importing python files (utility.py) located in the same directory as main.py script
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.
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.
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)
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.
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!
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.
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)