-
Notifications
You must be signed in to change notification settings - Fork 10
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
Comments
I should add that there is an "absolute path" solution to this which is : import sys 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 Otherwise to add the relative path to 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. |
You can use 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 |
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. |
Hi, Just released 1.4.0, and the workspace folder(s) are now added to the Python path by default. I've also added support for relative imports, which is most likely what you'd like to use if you're writing a package, Assuming you have a structure like:
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) |
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.
The text was updated successfully, but these errors were encountered: