I am creating a terminal game and I am attempting to import functions from my other files. I have searched through multiple forums, read through documentation, and either I can't wrap my head around it or I am doing it all wrong (Or just as likely both). I continuously have gotten a "ModuleNotFoundError: No module named 'scripts.gameCombat'/'gameCombat'" and also "ImportError: Attempted relative import with no known parent package"
#๐ Importing from a script to another in a github repo
48 messages ยท Page 1 of 1 (latest)
@cunning trench
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
gamemap is inside scripts
you'll have to do import scripts.gamemap
same for gameparty
It doesn't work at all is the problem, it doesn't work for line 1
how are you running the file?
Directly from VSCode with the run button
It is a remote repository, I am not sure just how much of a difference it makes
Ok, cloned it works just fine, but it doesn't work as a remote repo for idk why?
can you show its full output in the terminal? starting from the line that ran the command, e.g. C:\my\cwd> C:\path\to\python.exe C:\path\to\main.py
also not sure about a "remote repo" means here, is it like a temporary clone from the github extension?
essentially, it's an extension in VSC that lets you work on a repo without cloning it entirely
like a workspace?
yeah
I have seen trouble with running files with it in the past, so I guess it isn't a major surprise it works when cloned locally and not there, but it makes me wonder if it would happen in other scenarios
^ would still be interested in this output to know what's causing the issue
I hadn't thought to try a local clone first though
is it the "Virtual Repositories for Visual Studio Code" extension?
https://open-vsx.org/extension/carlocardella/vscode-VirtualRepos
Yeah, what is the statement for that?
Yes
when you clicked the run button, it should have opened your terminal and shown an output like: py (.venv) C:\Users\home\Documents\GitHub\joblin>c:/Users/home/Documents/GitHub/joblin/.venv/Scripts/python.exe c:/Users/home/Documents/GitHub/joblin/main.py Traceback (most recent call last): File "c:\Users\home\Documents\GitHub\joblin\main.py", line 1, in <module> raise Exception("Hello world!") Exception: Hello world!
ugh, I realized it doesn't actually print to terminal, it's just in a window from an extension, I've probably been mistaken this whole time
It's not an actual terminal
that looks like a jupyter notebooko
^
brain see arrow brain think run ๐
wait, so when you adjusted your imports did you push to your repo and pull back the changes?
actually maybe its jupyter having a different sys.path than normal
No, I was just changing it in the environment and testing it
i dont use it enough to know what it picks by default, but could you run the following from the notebook? py import sys print(sys.path)
['c:\Users\NAME\AppData\Local\Programs\Python\Python311\python311.zip', 'c:\Users\NAME\AppData\Local\Programs\Python\Python311\DLLs', 'c:\Users\NAME\AppData\Local\Programs\Python\Python311\Lib', 'c:\Users\NAME\AppData\Local\Programs\Python\Python311', '', 'C:\Users\NAME\AppData\Roaming\Python\Python311\site-packages', 'C:\Users\NAME\AppData\Roaming\Python\Python311\site-packages\win32', 'C:\Users\NAME\AppData\Roaming\Python\Python311\site-packages\win32\lib', 'C:\Users\NAME\AppData\Roaming\Python\Python311\site-packages\Pythonwin', 'c:\Users\NAME\AppData\Local\Programs\Python\Python311\Lib\site-packages']
is the output
hm, given the empty string '' that likely means jupyter didn't run main.py directly, which ig makes sense since it needs to be interactive
what about its current working directory? py import os print(os.getcwd())
"C:\Users\NAME\AppData\Local\Programs\Microsoft VS Code"
jeez, how awkward
guess that explains why it couldnt import your scripts
the empty string in sys.path is an alias for the CWD, so that means import gameCombat was trying to look for a gameCombat module in your VS Code installation directory
oh, I see what you mean by awkward then lol
Meanwhile, it runs like normal in a cloned repository
so it just boils down to how jupyter end up handling running the file?
the default terminal in contrast uses the open folder of your workspace as the CWD, but more importantly it runs the script directly with python path/to/main.py, adding path/to/ to sys.path
https://docs.python.org/3/library/sys.html#sys.path
i guess the jupyter vscode extension expects you to configure and install your source code with pip so its available from site-packages/
thank you for helping clear this up for me, I'm pretty inexperienced with using VSC, github, and the like
np, knowing how python resolves imports with sys.path and how to check it yourself will help you deal with broken absolute imports later on
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.