#๐ Help with import of module
199 messages ยท Page 1 of 1 (latest)
@swift briar
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.
...
..
do i need to paste the code into the https://paste.pythondiscord.com/ link to showcase it?
!code
if it's long then yes.
ah ok, so i pasted it in, what do i do next? sorry im new hehe
copy the link of the tab after you paste it and put the link here
i dont see you even use it
uhh quick tip its always best to put all the imports in the top,
btw whats the error?
CAPItols matter
Traceback (most recent call last):
File "c:\Users\MrPhil.vscode\extensions\ms-python.python-2024.14.1-win32-x64\python_files\python_server.py", line 130, in exec_user_input
retval = callable_(user_input, user_globals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'Question'
you made the questions moduel?
So i have my other file name exactly named "Question.py"
yeah i have the questions module as another file in the same directory
umm.. so like this
you want to import the whole thing?
then just one level import..
to use would be, Question.func in the module
hmm im not sure i understand
show questions.py
i need to know what your trying todo
if each question is a func or what
Im trying to get the class function that i made in the other file "Question.py" to work in my primary script by importing it, so i can use the defined parameters of the questions class to continue in my primary script
Im bowing out.. i dont use classes.. but perhaps for correct moduel use you need to also add if name main to it.
I'm a bit confused as to what's going on here.
So you have 2 files in the same directory. One named whatever your main script is, and another named Question.py or is it a directory called Question with an init file inside?
i also dont know much about classes :/
correct, 2 files. one is my main, another is the Question.py. The Question.py is where i have defined the class. and im trying to use or import that class into my main script
for anyone new to the thread, im trying to get my code at line 490 to work by importing my class named Question from another file called Question.py
so your file structure looks something like this:
.
โโโ main.py
โโโ Question.py
And Question.py is https://paste.pythondiscord.com/T54A
Are you running main.py from the same working directory as the question file is in?
correct. Yes i am running main.py from the same working directory
6 yellow :/
6 yellow is from previous code before this part of the script at line 490. it should not interfere?
should'nt the sudents name be in a string format?
python is not able to recoginize your module
maye this is not working
maybe hehe, but that part of the code is not belonging to this thread ๐
do i just make a filename called that? and leave the file empty?
yes
if that also doesn't work then idk what to say X_X
what error are you running into?
running into this error:
can you comment out all the code and try this:
import os
print(os.getcwd())
comment out all the code? (im new and not sure what this refers to), should i show all the code i wrote or the output of the code in the terminal?
"commenting" it means put # before each line so python ignores it
I'm not familiar with vsc so I'm not sure the hotkey to do it
looks like it might ctrl + K
and then ctrl + shift + K to uncomment after
ctrl + /
is that the folder where your Question.py is?
was this always in your code?
or did copilot add it?
output from os.getcwd is not the same directory as my file no. I dont know why it goes there
copilot did not add any no ๐ I manually write it all
is this the folder where your Question.py is?
yes
can you show the path at the top of the folder here?
ok and can you try running your code in vsc and then send a screenshot of your full window without cropping anything
lets see the top of the file
ideally you should be importing everything at the start
also did you install python from the microsoft store?
or more correctly, like this:
i installed it from the extensions tab which led me to the microsoft store i think so
lets see the top of the Hello World file
but also I highly recommend uninstalling python and then downloading it from python.org instead
top of file:
does that work from visual studio too? ๐
yes, a python install is system wide
alrighty, i will attempt to uninstall and reinstall from.org again. Is it sufficient to remove the extension and uninstall from system program/files folder?
I'm not certain about the extension because I don't use vsc ๐ฆ
but couldn't hurt to have a clean install!
they already did it 2 times
doing a clean uninstall then, will return in a few min when done
they're using microsoft store python though
ahh makes sence
when installing again, im asked to select an interpreter. What do i chose here?
installing python or the extension?
yes, select that python 3.12 interpreter
what settings did you choose for your python install?
There were some important ones in there
Couldn't what's happening here be that VSCode runs the script in a different working directory than where the Question and Hello World files are?
we checked cwd already
where in your code is this line?
i just chose the installer and i didnt give me any options to chose?
it should have given you options during the installation process
Now it shows the correct path at least, but code is still not accepted, same error - cant find module
this error looks like it's still trying to run the old interpreter
did you restart vsc?
what if i try to make a completely new file then with the same code?
yup
worth a shot. I'd avoid using spaces in your file names btw
hello_world.py instead
I'm not really certain how vsc does things but is there a reason you're using the Python REPL and not the terminal at the bottom?
also moving your folder structure is going to lead to import issues too
putting Myfile into a folder is not the same level that Hello World was at
i just recreated the exact scripts at another file location
ok except now you have Question2 and you're still just trying to import Question
ah yes, i should correct that too ๐
but it seems like this whole issue is caused by the .ipynb (notebook) file
is there a reason you're using that?
no, what is that?
it's a different way of running code in steps
you should be using the terminal to run your code
by using the terminal:
try running the original this way
i can't run the original that way because it contains faulty code above this segment
what do you mean by that?
comment out your entire code
then JUST do import Question
my shortcut for commenting out dosent work, how to do?
try ctrl + /
ctrl + /
ctrl + / dosent work :/
there might be an option in the menus at the top
maybe under Edit
and hopefully it also has the hotkey listed next to it
toggle line comment? that seems to work on the selection i made
yes that's it
so should i i comment out everything or just the code i dont want to focus on?
what were you clicking before...?
control shift to run the highlighted selection only
so importing only works by running with the arrow/terminal function? all previous code from 4 hour course worked that way
If you run only the highlighted section using notebooks, the notebook runtime might run with a bad environment.
In this case, the notebook didn't even know your Question.py file existed.
yeah I never really work with notebooks so I had no idea
i dont even know what are those XD
but generally you should write your py files to run the entire thing at once
and if you only want to run a portion of it, those could be put into functions and just call what's necessary
ahh okay, im trying to clean things up and get the script to run
so this happens when i run it:
go back to using your original files
you're using chdir and that's not where your files are
but it is where your original files are
sadly i cant run the code using the arrow option in my original script because it contains faulty code further up that it will stop on beforehand
so i made a clean one
yes but you made it in a different location
this is at my original file with all other code commented out
i dont understand the output, should it not give me an option to input something for the question if the import worked?
after restarting:
unless your forgot to make an input()
you aren't calling your runtest function
true
question: does it automatically identify the class from same directory without the need to import it? because just writing the file seems to pop up with the correct definitions.
hmm?
when you just import the module it will import everything but you have to call that function
when you import somthing from the module it will just import only that specific function (idk if this explaination is correct)
with the added runtest. im super confused haha
oh nevermind i forgot to remove other code
like you just imported Question to use the question class in side the question.py you have to use Question.Question
it works!
what is the Python.REPL file?
Thanks alot for the support, help is very appreciated
that was created by your notebook / running only the selected line and should be safe to delete, unless you want to keep it.
Then i'll delete that annoying file causing the trouble haha
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.
๐ Help with import of module