0

I'm developing a chatbot using heroku and python. I have a file fetchWelcome.py in which I have written a function. I need to import the function from fetchWelcome into my main file.

I wrote from fetchWelcome import fetchWelcome in main file. But because we need to mention all the dependencies in the requirement file, it shows error. I don't know how to mention user defined requirement. How can I import the function from another file into the main file ? Both the files ( main.py and fetchWelcome.py ) are in the same folder.

2
  • Consider dropping "Heroku" from the title of this question. Commented Mar 29, 2018 at 17:03
  • @ChadVanDeHey I know how to import function from another files when working locally. I need to do the same when I deploy my code on Heroku server. Commented Mar 30, 2018 at 7:03

2 Answers 2

2

You're quite close to the answer to the question. Importing works like this:

fetchWelcome.py:

def foo():
    # Do something here
    print("Hello World")

def bar():
    # Do something else
    print("Python")

main.py:

import fetchWelcome

fetchWelcome.foo()
fetchWelcome.bar()

If you only want to import a single function, use

from fetchWelcome import foo

foo()

Both files have to be in the same folder.

Sign up to request clarification or add additional context in comments.

3 Comments

The answer you posted is correct but I need to know how can I do the same when I deploy the code on Heroku server.
Please edit your question and provide additional information. What are you trying to do? Which files are in which folder? Post your "requirement file" and your shortened source code.
@fecavy Found the answer. If we need to import function from fetchWelcome.py into main.py, write "from .fetchWelcome import functionName". Thus we don't need to write any dependency in requirement file.
0

If we need to import function from fileName into main.py, write

from .fileName import functionName

Thus we don't need to write any dependency in requirement file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.