2

Does anyone know how to run python script with python from virtual environment without cmd. In Linux it is easy with shebang.

Problem is that I have installed Python2 and on MyProject I have created virtual environment in Python3. Now when I click on main.py in MyProject I get syntax error and that is normal because it calls Python2 and in main.py I use async def that in Python2 doesn't exist.

What I must add in main.py so that it automatically recognises that it should be executed by python3 located in virtual environment and not with windows assigned default python2.

I don’t want to use cmd and I want all in main.py that is needed to run that py on click.

Any ideas?

PythonMan
  • 787
  • 10
  • 18

1 Answers1

-1

Check the link: Detect Python version at runtime

You can add the following lines in your code.

import sys
if sys.version_info[0] < 3:
    raise "Must be using Python 3"
else:
    from your_script import your_method
    your_method()
  • Ok but that doesn't solve anything and this answer has nothing with solution to my question.... This just checks version of current python. and the SyntaxError will still be thrown since before you can even check version it must pass syntax check from interpreter so that you know. – PythonMan Apr 10 '18 at 10:25
  • call your script method in else block. –  Apr 10 '18 at 10:59