This tutorial explains How to create a Module and import Python code in Pycharm.

The module is a Python file that contains reusable code used in other Python files. When you create a Python file as a module, Pycharm automatically recognizes and use it as a module and give hint and suggestion in Pycharm Code Editor.

How to Create a Module in Pycharm Editor

To create a module in Pycharm IDE, follow the below steps

  • Open Pycharm IDE from the command line or UI Options
  • Next, Create a New project or open an existing project
  • To create a new project
  • Go to File Menu, and Select New Project, It opens the New Project Window
    • Enter location, location contains workspace and name of the project
    • Select Base Interpreter, Python version to choose You can check the below screenshot for reference

pycharm New project

  • It creates a new project, Select Project in the Project Window

  • Right Click on it, Shows Context Menu, Select New -> Python File option

  • Popup Shown and enter the name of the module calculator_module and select Python File, Click OK

  • calculator_module.pycreated and shown in the Project window under a project directory, also open the file in the code editor, the Name of the module is calculator_module.

  • Write below code calculator_module.py file

def add(a, b):
    return a+b;
  • Now, Created a module and added an add function.
  • Next, reuse the module code in other files using import module in the code
  • For example, main.py file, add the first line with import followed by code suggestion hint name of the module, select calculator_module and it is shown as given below
import calculator_module;

next, call the function in the

import calculator_module;
result=calculator_module.add(10,20);
print(result) #30