1

I developed an application with Python 3 that produces various executables. I then used setuptools to build and distribute this application, again all using Python 3.

When this application is installed in a test environment, the executables are being correctly deployed to the bin folder and thus become invokable from anywhere in the system. However, when these executables are invoked, the system tries to use the Python 2 interpreter, leading to an exception. How can I make sure the Python interpreter is used when I invoke these executables?

glibdud
  • 7,550
  • 4
  • 27
  • 37
Luís de Sousa
  • 5,765
  • 11
  • 49
  • 86

2 Answers2

0

I made sure install was run with Python 3 and that the resulting script included the correct header. Still I kept getting exceptions from Python 2.7.

Out of desperation I created a new Python 3 virtual environment and in it the scripts started working properly. There are previous reports of old virtual environments going haywire, particularly during a system upgrade.

For future reference, the command I used:

mkvirtualenv -p /usr/bin/python3.5 venv_p3

Community
  • 1
  • 1
Luís de Sousa
  • 5,765
  • 11
  • 49
  • 86
-2

You might need to use bash shebangs on your scripts, which are small strings at the beginning that specifies what binary should interpret them.

In your case you need to add #!/usr/bin/env python3 at the beginning of your scripts. The bash shell should read this and pass the script to your python3 installled interpreter.

Example:

#!/usr/bin/env python3

# This should work on python3 and fail on python2:
print("Hello from python3!")  
Community
  • 1
  • 1
llekn
  • 3,271
  • 2
  • 18
  • 23
  • The scripts placed in bin are usually generated from the defined entry points in `setup.py` by setuptools, the shebang is autogeneratet pointing to the interpreter used during installation. And a "bash shebang" is `#!/bin/bash`, it should be only "shebang" in this context, and it's not bash (or other shell) interpreting that line but the os. – mata Sep 26 '16 at 14:01