Python Forum

Full Version: Python-Can I get some help with this ModuleError
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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'
What is k?
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
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.
Thanks for the help again guys
(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?
Why did you use "import k as k" or "import k" for that matter?
(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.
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.
(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.
Pages: 1 2