0

Using a Debian-based Linux distro (Kali) for Python programming (note that I am very new to Linux, had it for less than 2 months). Installed Python and GitKraken the usual way with apt-get install.

Started happily coding using the 3.5.3 IDLE, but when I tried to use pip commands to install modules (all of this as root user), I found it was installing to /usr/local/lib/python2.7/dist-packages rather than the Python 3.5.3 location (/usr/local/lib/python3.5/dist-packages).

When I used pip install module, it installed all modules to the 2.7 location. Because the Terminal used 2.7 by default (for whatever reason), using ./code.py always ran code using Python 2.7, but I wrote the code for Python 3.5 (not the version the modules were installed for).

I saw some other answers on this website for similar problems, where you have to install new modules and change the PYTHONPATH variable and assign aliases, but it messed more stuff up. Now echo $PYTHONPATH returns nothing, and pip still installs to the Python 2.7 location.

I used apt-get to install python-pip3 and I use the pip3 command to install modules, but whenever I use ./code.py (my main method to run code), it still uses Python 2.7. How can I change this?

1 Answers1

0

Edit the shebang at the first line of code.py to point to your Python 3.5 binary, e.g.:

#!/usr/bin/env python3

Or if it's not linked as python3 figure out were your Python 3.5 binary is and use that.

zwer
  • 24,943
  • 3
  • 48
  • 66
  • Originally had `#!/usr/bin/python`, had to add a 3.5 at the end. Now (with aliases for `pip` and `python`), everything runs good. –  Feb 17 '17 at 20:47
  • Using env as in the answer is preferable because it is more portable - the install location of env is well standardized across unix systems and linux distributions, the location of (and exact sub-version) of python is not. `/usr/bin/env python3` is more likely to work on another system than `/usr/bin/python3.5`. Even if the other system has `python3` in the path, it could be in `/bin` or `/usr/local/bin`, and it could be `python3.6` or some other name, etc. – zstewart Feb 17 '17 at 22:25