Python Forum
Python-Can I get some help with this ModuleError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python-Can I get some help with this ModuleError
#1
The program works fine if I run it on Pycham but if I try to run it on IDLE 3.9 it displays an error. I already try the (pip install k) but It don't work or maybe I no typing it correctly? or in the right place.


import random
import k as k

def linearSearch(list1,k):
    i=0
    n=len(list1)


    for i in range(n):
        print("index: ",i)
        print("comparing ",list1[i]," with ",k)
        if(list1[i]==k):
            print("\n",k," Found at index\n :",i)
            break
    else:
        print("\n",k," Not Found\n\n")


def randomListGenerate(len):
    list1=[]
    for i in range(len):
        k = random.randint(1,50)
        list1=list1+[k]
    return list1

k = random.randint(1,50)
list1 = randomListGenerate(10)
print("List is :",list1)
print("Element to search: ",k)
linearSearch(list1,k)
list1 = randomListGenerate(10)
list2 = randomListGenerate(10)

print("\nList1 is :",list1)
print("List2 is:",list2)
print("\nsearching each element of list1 in list2:")
for k in list1:
    linearSearch(list2,k)
Output:
Traceback (most recent call last): File "C:/Users/Alex/Desktop/test.py", line 2, in <module> import k as k ModuleNotFoundError: No module named 'k'
Reply
#2
What is k?
AlexPython and buran like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Is k a module (py file) located next to your script in PyCharm project folder? By the way, poor name and also no point of importing with alias i.e. import k as k
If this is the case - the current working directory from which you run the script in IDLE is different from the one in PyCharm
AlexPython likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
You use k as a module name and you use it as an integer.

As a module name
import k as k  # Should be import k, not import k as k
And here as an integer
k = random.randint(1,50)  #Now k is an int.  Module k is gone!
My guess is you do not have a module named k that you want to import. I think you believe "import k as k" is defining a global variable named "k". You never treat k like a module. There is no x = k.function(args) or y = y + k.value type references to the module "k" in your program. And as soon as you assign k an integer value, you have no way to reference the module anymore.

Your program works fine if you delete the "import k as k" line.
AlexPython likes this post
Reply
#5
Thanks for the help again guys
Reply
#6
(Sep-23-2022, 12:20 AM)menator01 Wrote: What is k?

k is the element the code will be searching in each of the lists. I guess I should change the name? any recommendation?
Reply
#7
Why did you use "import k as k" or "import k" for that matter?
AlexPython and buran like this post
Reply
#8
(Sep-24-2022, 09:07 AM)deanhystad Wrote: Why did you use "import k as k" or "import k" for that matter?

I was having some problems with the code because it was saying that "K" dint have value or something like that. So how did I get to "import k as k"? I don't know if you use pycham, but they have a little window of tips and recommendations for the code being written. I hit the format button and they automatically put that code line in and then the code was working. so I assume that was the problem. I don't know if they fix other things like indentation issues or something that probably was the real problem.
Reply
#9
Accepting hints that you don't understand is a bad idea. I think using hints at all, at this point in your learning, is a bad idea. Pycharm saw you using an unassigned variable and decided it might be a module and offered to write the import for you. You can configure VSCode to do the same thing. I hate that feature with a burning passion and turned it off after using it for one day.
AlexPython likes this post
Reply
#10
(Sep-24-2022, 08:28 PM)deanhystad Wrote: Accepting hints that you don't understand is a bad idea. I think using hints at all, at this point in your learning, is a bad idea. Pycharm saw you using an unassigned variable and decided it might be a module and offered to write the import for you. You can configure VSCode to do the same thing. I hate that feature with a burning passion and turned it off after using it for one day.

Yeah, I realized that in the long run, that feature would do more harm than good to me.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020