#๐Ÿ”’ Importing from a script to another in a github repo

48 messages ยท Page 1 of 1 (latest)

cunning trench
#

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"

clever grottoBOT
#

@cunning trench

Python help channel opened

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.

analog quail
#

gamemap is inside scripts

#

you'll have to do import scripts.gamemap

#

same for gameparty

cunning trench
#

It doesn't work at all is the problem, it doesn't work for line 1

analog quail
#

how are you running the file?

cunning trench
#

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?

north arrow
#

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?

cunning trench
#

essentially, it's an extension in VSC that lets you work on a repo without cloning it entirely

analog quail
#

like a workspace?

cunning trench
#

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

north arrow
cunning trench
#

I hadn't thought to try a local clone first though

north arrow
cunning trench
north arrow
# cunning trench Yeah, what is the statement for that?

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!

cunning trench
#

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

analog quail
#

that looks like a jupyter notebooko

north arrow
#

^

cunning trench
#

brain see arrow brain think run ๐Ÿ’€

north arrow
#

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

cunning trench
#

No, I was just changing it in the environment and testing it

north arrow
#

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)

cunning trench
#

['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

north arrow
#

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())

cunning trench
#

"C:\Users\NAME\AppData\Local\Programs\Microsoft VS Code"

north arrow
#

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

cunning trench
#

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?

north arrow
#

i guess the jupyter vscode extension expects you to configure and install your source code with pip so its available from site-packages/

cunning trench
#

thank you for helping clear this up for me, I'm pretty inexperienced with using VSC, github, and the like

north arrow
#

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

clever grottoBOT
#
Python help channel closed

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.