Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python How to import and use a module from another Python program online?

Hi All

I'm learning Python.

I have created a typical sample module online (specifically on repl.it):

[CODE lang="python" title="module example"]# Python Module example

def add(a, b):
"""
This program adds two
numbers and returns the result.
"""

return a + b
[/CODE]

Here, the module file name is example.py, the module is named example and it contains a sample function add(a, b) which returns the sum of the parameters.

I have no problem accessing this sample function from file main.py in the same program. It works perfectly, once the import is done:

[CODE title="import example module - same program"]
import example
print("example.add(2, 12) = ",
example.add(2, 12))
[/CODE]

However, when I try to access module example from another program, it is not possible, since the Python interpreter can't locate example module (screenshot attached).

How can I do this (assuming the Python program accessing example module will be distinct from the program containing function add()?

I am asking this particularly in the context of online websites (such as repl.it) where I can create such programs entirely online.
 

Attachments

  • import-example.jpg
    import-example.jpg
    141.5 KB · Views: 2

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom