Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save plieningerweb/39e47584337a516f56da105365a2e4c6 to your computer and use it in GitHub Desktop.
Save plieningerweb/39e47584337a516f56da105365a2e4c6 to your computer and use it in GitHub Desktop.
Restart python script itsself (run again).md
@ivanachille
Copy link

works for python 3?

@Suzdalev
Copy link

Works with python3

thank you, author, for this thing

@Techtiger255
Copy link

Techtiger255 commented Mar 27, 2020

Works like a charm (--a pycharm?)! I am running a discord bot that I quite frequently manage, and having a command like this is extremely helpful! I've looked everywhere and this is the only one that's worked so far! One thing that I'd change just for optimization's sake:
['python'] + sys.argv -> ['python', sys.argv] as the + operator is not optimized for this purpose in python...
nevermind, it doesn't work if you do that... Do you know why that happens @plieningerweb?

@malmeloo
Copy link

@Techtiger255 Because that's not how you concatenate lists in python:

>>> my_list = ['foo', 'bar']

>>> print(['spam', my_list])
['spam', ['foo', 'bar']]

>>> print(['spam'] + my_list)
['spam', 'foo', 'bar']

>>> ['spam', my_list] == ['spam'] + my_list
False

Alternatively, you could do my_list.extend(['spam']), however this modifies my_list and might not be what you want.

@shadowpants18
Copy link

I'm getting connectionRefusedError: [WInError 10061], anyone know why?

@Anandoham
Copy link

Thank you for this!

@andreapramadityaperwiraputra
Copy link

thanks for this information dude! it's working for my project, thanks again.

@dante-nl
Copy link

dante-nl commented Feb 8, 2021

Amazing, thank you!

@dante-nl
Copy link

dante-nl commented Feb 8, 2021

Seems like the input function is broken with it tho...

@YYx00xZZ
Copy link

if you want to run/restart your script with -i its not working

@heyjoeway
Copy link

Wouldn't it make more sense to not hardcode the executable name?

import os, sys
def restart():
    os.execv(sys.executable, [os.path.basename(sys.executable)] + sys.argv)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment