#๐Ÿ”’ Help with import of module

199 messages ยท Page 1 of 1 (latest)

shut willowBOT
#

@swift briar

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.

naive merlin
#

...

icy current
#

..

swift briar
naive merlin
#

!code

shut willowBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

swift briar
#

ah ok, so i pasted it in, what do i do next? sorry im new hehe

icy current
#

copy the link of the tab after you paste it and put the link here

swift briar
#

we are on line 490

naive merlin
#

i dont see you even use it

icy current
#

uhh quick tip its always best to put all the imports in the top,
btw whats the error?

naive merlin
#

CAPItols matter

swift briar
#

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'

naive merlin
#

you made the questions moduel?

swift briar
#

So i have my other file name exactly named "Question.py"

#

yeah i have the questions module as another file in the same directory

naive merlin
#

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

swift briar
#

hmm im not sure i understand

naive merlin
#

i need to know what your trying todo

#

if each question is a func or what

swift briar
#

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

naive merlin
#

Im bowing out.. i dont use classes.. but perhaps for correct moduel use you need to also add if name main to it.

lyric saffron
#

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?

icy current
#

i also dont know much about classes :/

swift briar
#

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

lyric saffron
icy current
#

6 yellow :/

swift briar
#

6 yellow is from previous code before this part of the script at line 490. it should not interfere?

icy current
#

should'nt the sudents name be in a string format?

icy current
swift briar
icy current
#

maybe trying making a __init__.py file

swift briar
icy current
#

if that also doesn't work then idk what to say X_X

swift briar
#

hmm sadly same error on this:

sacred tapir
icy current
swift briar
#

running into this error:

sacred tapir
#

can you comment out all the code and try this:

import os

print(os.getcwd())
swift briar
#

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?

sacred tapir
#

"commenting" it means put # before each line so python ignores it

swift briar
sacred tapir
#

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

icy current
swift briar
sacred tapir
#

was this always in your code?

#

or did copilot add it?

swift briar
swift briar
sacred tapir
sacred tapir
#

then it should be working now

#

it's still the same error?

swift briar
#

yeah its still the same error

#

can it be due to a version of Python in visual?

sacred tapir
swift briar
sacred tapir
#

ok and can you try running your code in vsc and then send a screenshot of your full window without cropping anything

swift briar
sacred tapir
#

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?

swift briar
#

or more correctly, like this:

swift briar
sacred tapir
#

but also I highly recommend uninstalling python and then downloading it from python.org instead

swift briar
#

top of file:

swift briar
sacred tapir
swift briar
#

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?

sacred tapir
#

I'm not certain about the extension because I don't use vsc ๐Ÿ˜ฆ

#

but couldn't hurt to have a clean install!

icy current
swift briar
#

doing a clean uninstall then, will return in a few min when done

sacred tapir
icy current
swift briar
#

when installing again, im asked to select an interpreter. What do i chose here?

sacred tapir
#

installing python or the extension?

swift briar
#

hmm when i tried to open my main python script again

sacred tapir
#

yes, select that python 3.12 interpreter

#

what settings did you choose for your python install?

#

There were some important ones in there

lyric saffron
#

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?

sacred tapir
swift briar
sacred tapir
swift briar
#

Now it shows the correct path at least, but code is still not accepted, same error - cant find module

sacred tapir
#

this error looks like it's still trying to run the old interpreter

#

did you restart vsc?

swift briar
swift briar
sacred tapir
#

hello_world.py instead

swift briar
#

i tried with the same error

sacred tapir
#

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

swift briar
#

i just recreated the exact scripts at another file location

sacred tapir
#

ok except now you have Question2 and you're still just trying to import Question

swift briar
#

ah yes, i should correct that too ๐Ÿ˜„

sacred tapir
#

but it seems like this whole issue is caused by the .ipynb (notebook) file

#

is there a reason you're using that?

swift briar
#

no, what is that?

sacred tapir
#

it's a different way of running code in steps

#

you should be using the terminal to run your code

swift briar
#

by using the terminal:

lyric saffron
#

try running the original this way

swift briar
#

i can't run the original that way because it contains faulty code above this segment

sacred tapir
#

what do you mean by that?

#

comment out your entire code

#

then JUST do import Question

swift briar
#

my shortcut for commenting out dosent work, how to do?

sacred tapir
#

try ctrl + /

swift briar
#

ctrl + / dosent work :/

sacred tapir
#

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

swift briar
#

toggle line comment? that seems to work on the selection i made

sacred tapir
#

yes that's it

swift briar
#

so should i i comment out everything or just the code i dont want to focus on?

sacred tapir
#

comment out everything

#

your only line of code that runs should be import Question

swift briar
sacred tapir
#

is this what you're clicking to run it?

swift briar
#

aha

sacred tapir
#

what were you clicking before...?

swift briar
#

control shift to run the highlighted selection only

icy current
#

no wonder it was not working X_X

#

before.... (useful info :D)

swift briar
#

so importing only works by running with the arrow/terminal function? all previous code from 4 hour course worked that way

lyric saffron
#

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.

sacred tapir
#

yeah I never really work with notebooks so I had no idea

icy current
#

i dont even know what are those XD

sacred tapir
#

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

swift briar
#

ahh okay, im trying to clean things up and get the script to run

#

so this happens when i run it:

sacred tapir
#

you're using chdir and that's not where your files are

#

but it is where your original files are

swift briar
#

so i made a clean one

sacred tapir
#

then don't run faulty code?

#

fix that code

#

or comment it out

sacred tapir
swift briar
#

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?

sacred tapir
#

try restarting vsc

#

it seems to be running an old version of the sript

swift briar
#

after restarting:

icy current
sacred tapir
#

you aren't calling your runtest function

icy current
#

true

swift briar
#

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.

icy current
swift briar
#

oh nevermind i forgot to remove other code

icy current
#

like you just imported Question to use the question class in side the question.py you have to use Question.Question

swift briar
#

it works!

#

what is the Python.REPL file?

#

Thanks alot for the support, help is very appreciated

lyric saffron
swift briar
#

Then i'll delete that annoying file causing the trouble haha

shut willowBOT
#
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.

#

๐Ÿ”’ Help with import of module