bserce123
Posts: 43
Joined: Mon May 22, 2017 3:28 pm

importing a .py script into another script

Mon Jul 10, 2017 1:36 am

I am trying to learn how to call one .py script from another.
I have a python script 'hiya.py'. This contains only one line:- print('hello you'). From the terminal the command python3 hiya.py displays 'hello you'. So i have another script 'doit.py' which has two lines:-
import hiya.py
python3 hiya.py
This doesn't work. So if I turn the code in 'hiya.py' into:-
def writit():
print('hello you')
return

and change 'doit' to:-
import hiya.py
writit()
it still doesn't work.

So would some kind person give me the code that would work?

NotRequired
Posts: 196
Joined: Sat Apr 29, 2017 10:36 am
Location: Denmark

Re: importing a .py script into another script

Mon Jul 10, 2017 10:24 am

You should not add ".py" to your import section. Look here for more info on python modules: https://docs.python.org/3/tutorial/modules.html
Please do not ask questions in private messages, they will not help others.

bserce123
Posts: 43
Joined: Mon May 22, 2017 3:28 pm

Re: importing a .py script into another script

Mon Jul 10, 2017 1:49 pm

NotRequired wrote:You should not add ".py" to your import section. Look here for more info on python modules: https://docs.python.org/3/tutorial/modules.html
Many thanks for that, it is exactly what I needed, I had previously been reading up 'import' and had got horribly lost.

Return to “General discussion”