3

When I run the terminal then access to python3 shell, I can run a file or module using import, but if I try to run it again, nothing happens.

I saw this question before: [Need to restart python in Terminal every time a change is made to script

and I read the docs: [https://docs.python.org/3/tutorial/modules.html#executing-modules-as-scripts][1]

but both are talking about restarting a single function in the module. I am talking about rerunning the whole file. I included this code in the end of my file, but still nothing happened

if __name__ == "__main__":
        pass

UPDATE: After I ran the file as in the comments, this is what I got:

Ms-MBP:mine M$ python3
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> python python_file.py
  File "<stdin>", line 1
    python python_file.py
                     ^
SyntaxError: invalid syntax
>>>
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • The way to run a script is to run the command `python my_python_file.py` in the terminal. – Alex Hall Aug 19 '17 at 21:38
  • 1
    `import` is not intended as a "run this file" system. Have the file define a function to perform its functionality, and call the function. – user2357112 Aug 19 '17 at 21:44
  • Thanks everyone, but I meant how to run it in the interpreter many times not from the normal terminal. –  Aug 19 '17 at 21:55
  • @iCoder you *should* not paste the answer into your question. The answer is below, everyone can see it. You *must* not delete a relevant part of your question when doing so, because afterwards, the answers don't make sense anymore. – Marcus Müller Aug 31 '17 at 22:21

1 Answers1

7

As mentioned in the comments, if you're working in a terminal you should use the command (remember to do this from the main shell, ie probably bash, not from the Python shell)

$ python script.py

This is the intended way to execute python files. You can normally quickly cycle back to the previous command with the UP arrow on your keyboard.

However, if you for some reason really need to run your script from the interactive interpreter, there are some options, although beware that they are kind of hacky and probably not the best way to go about running your code, although this may vary with what your specific use case is.

If your script, hello.py, had the following source:

print("hello world")

In python3 you could do the following from the shell:

>>> from importlib import reload
>>> import hello
hello world
>>> reload(hello)
hello world
<module 'hello' from '/home/izaak/programmeren/stackoverflow/replrun/hello.py'>

Here is the documentation for importlib.reload. As you can see, this replicates the side effects of the script. The second part is the repr() of the module, as the reload() function returns the module - this is nothing to worry about, it is part of the way the interpreter works, in that it prints the value of anything you enter into it - eg you can do

>>> 2 + 3
5

rather than having to explicitly print(2 + 3). If this really bother you, you could do

>>> from importlib import reload as _reload
>>> def reload(mod):
...     _reload(mod)
... 
>>> import hello
hello world
>>> reload(hello)
hello world

However, it would be more idiomatic for your script to look something like this, using that if statement you found (this was also a suggestion in the comments):

def main():
    print("hello world")

if __name__ == "__main__":
    main()

This way, from the Python shell you can do:

>>> import hello
>>> hello.main()
hello world
>>> hello.main()
hello world

This is very good practice. The if statement here checks if the script is being executed as the 'main' script (like running it directly from the command line, as in my first suggestion), and if so it executes the main function. This means that the script will not do anything if another script wants to import it, allowing it to act more like a module.

If you're using IPython, you'll probably know this but this becomes a lot easier and you can do

In [1]: %run hello.py
hello world
Izaak van Dongen
  • 2,450
  • 13
  • 23
  • Thanks a lot. But is there any differences between running a script from the terminal or the interpreter. Or running it from the IDLE? –  Aug 19 '17 at 22:14
  • There isn't necessarily a difference, but the interpreter isn't really designed for running your scripts. To execute any python code you'd almost always want to use `$ python hello.py` in the terminal (bash), rather than any solution in the interpreter. If you're working in IDLE, you should be able to just run your file using some menu option (refer to https://stackoverflow.com/questions/6513967/running-python-script-from-idle-on-windows-7-64-bit) – Izaak van Dongen Aug 19 '17 at 22:20