skip to Main Content

I am learning Python and wanted to run one of Cormen’s textbook examples that are in Python.
I downloaded Python.zip from https://mitpress.mit.edu/9780262046305/introduction-to-algorithms/ Resources tab.
I extracted it and it is a number of folders, one per chapter.
I do not see an "import project" option in VSC.
Trying to run randomized_select.py from Chapter 9 folder in the debugger, I get error

ModuleNotFoundError: No module named 'randomized_quicksort'

It is coming from

from randomized_quicksort import randomized_partition
File c:UsersMeDocumentsLearnMS-CSFoundations of Data Structures and AlgorithmsCLRS_PythonChapter 9randomized_select.py:33
      1 #!/usr/bin/env python3
      2 # randomized_select.py
      3 
   (...)
     30 #                                                                       #
     31 #########################################################################
---> 33 from randomized_quicksort import randomized_partition
     36 def randomized_select(A, p, r, i):
     37     """Return the ith smallest element of the array A[p:r+1]
     38 
     39     Arguments:
   (...)
     43     i -- ordinal number for ith smallest
     44     """

ModuleNotFoundError: No module named 'randomized_quicksort'

I am stuck here.

2

Answers


  1. Looking into this found that

    ModuleNotFoundError: No module named ‘randomized_quicksort’

    This means in the current folder where your randomized_select.py is present, the module you are trying to import isn’t present there. Try creating then importing the module. This may fix your issue.

    To import a project in VS Code:

    1. From File select Open Folder

    2. Navigate to your folder you want to import

    Always use and place import statement in the starting of the code i.e., very first.

    Login or Signup to reply.
  2. Looking at the zip file, the randomized_partition module is in the Chapter 7 folder. In order for it to work, you will also need the quicksort module in the Chapter 7 folder.

    Put copies of all three files into the same folder, and then running python randomized_select.py, or using the debugger should work.
    You may need to adjust your launch.json settings so that it uses the target script’s directory as the working directory instead of the base folder of your workspace.

    An alternative workflow would be to copy the functions out of the quicksort.py and randomized_partition.py files into randomized_select.py, and then you can remove the import statement entirely, as long as there are no function name conflicts.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search