Terminal's python version and Eclipse's python version mismatch - python

My MacBook came preinstalled with Python 2.7.16, and I downloaded Python 3.8.5. To my understanding, the operating system needs Python 2.x so I did not uninstall it.
Eclipse (using Pydev) is the IDE I'm using.
I set up two interpreters, one for python and the other python3.
I set up one project for each interpreter to make sure I set them up correctly.
The script is:
import sys
print(sys.version)
When I run it with the python interpreter, I correctly get version 2.7.16.
When I run it with the python3 interpreter, I instead get 3.8.2.
Running python -V yields ``Python 2.7.16. Running python3 -VyieldsPython 3.8.5```.
Why does the interpreter return one version and the terminal another?
I'm at a loss for how to troubleshoot or fix this, or if it is a non-issue.

To clarify, you get the 2.7.16 and 3.8.2 versions when running your program from within Eclipse. The python -V is clearly from the command line. The interpretation is that your Eclipse environment came with its own python interpreter which happened to be 3.8.2. Have you tried running your script from the command line with python3 path/to/your/script.py? This probably gives 3.8.5. I don't see a real problem here in most cases you do not care whether you have python 3.8.2 or 3.8.5.
The "biggest" issue is a cosmetic one that you have two python3 installations which is a bit of a waste. When using additional libraries, you may find that you have to install them in your Eclipse environment and in your command line if you want to use your scripts in both environments which would be a bit tedious. If this does turn out to be a problem, check in Eclipse whether there is any way to change your python3 configuration to use the interpreter used by the command line (sorry cannot be more specific, it's a long time that I used Eclipse).

Related

How to run python script #!python3 command with specific sub version of python 3

I have two versions of Python 3.6.1 and 3.6.2
I have a sample script below:
#! python3
import arcpy
print ("Gurminder")
The code runs by default with 3.6.1 as I have specified it to run with python3
However is there any way that I can force the compiler to run the with python 3.6.2?
Is there any parameter where I can specify the subversion of the python interpreter to be used?
I am aware of running the python with its interpreter executables for example:
"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" D:\inetpud_backup_17_04_2018\Gurminder\Cases\02104091\test.py'
The command works fine in command prompt however when it is run using os or subprocess module I get the error for the white space between the Program Files
Hence on researching the issue, I found the workaround on specifying the version in the header. Is there any way to define the subversion as well?
You could use a direct path to Python 3.6.2 as your shebang line:
#!C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe"
I also see no reason why this is necessary. Python 3.6.1 and Python 3.6.2 are exactly the same except for the fixes.
There are no packages you cannot use in Python 3.6.1 and 3.6.2. Why not just use 3.6.2 instead?

environment with multiple python interpreter

On Mac, if installed multiple Python interpreter (anaconda, Python 2.7 standard, etc.), wondering when running command python, is there a way to specify if I run with anaconda or Python 2.7 or other Python interpreter?
If you only type python, it will take you to the version of python specified by this:
which python
This command will print out the location of python in your terminal.
To use a version other than the one above, be it Jython or Anaconda or whatever, you can to run it by typing out the fully qualified path.

Python code in OSX Terminal not compiling right

So I am trying to teach myself to program in Python while on spring break and I have run into a roadblock. I can get my code to compile in PyCharm, but I would really like to get it to compile correctly in Terminal because vim is my text-editor of choice. Does anyone have any idea of why my code may not be compiling correctly?
Here is my code compiling correctly in PyCharm
Here is my code compiling incorrectly in Terminal
Thank you in advance for any help.
Any recent OSX version comes with Python 2.7 as a standard. When you install Python 3.x, you have both versions on your system. The standard way of using python -command- in the terminal calls Python 2.7. You can call a command using python3 -command- instead to use Python 3.x. You could set an alias on python3 in .bash_profile to call it instead when you use python.
You have configured PyCharm to use an installed Python 3, but at the terminal you appear to be using Python 2. There is a difference in how the two versions output the results of print. Try modifying your terminal session to direct your path to use the installed Python 3 instead of the installed Python 2.

How can I make the "python" command in terminal, run python3 instead of python2?

I'm just starting to learn Python and did search around a little, so forgive me if this has been asked and answered.
When running scripts through the command line/terminal, I have to type "python3" to run the latest version of Python. With Python 2.X I just use "python".
Is there a way to run Python 3 just using "python"?
It may seem a little lazy, but I'm mostly just curious if it is possible or if it will break anything unnecessarily if I could in fact do it.
If you are using Linux, add the following into into ~/.bashrc
alias python=python3
Restart the shell and type python and python3 should start instead of python2.
If you're using Windows then you can use the Python Launcher For Windows.
This will allow you to use the py command to select different python installations such as:
py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)
Similarly you can set a shebang in your python files as demonstrated below:
#! python3
print('Hello World!')
If you now run that file (let's call it test.py) with py test.py it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.
What you probably want is to customise the default python version though. This will allow you to set the default actions if you just call py on it's own.
Once you installed python 3 in your Mac, "python3" command will be registered into the environment variable automatically. So if you need to run your python 3 file just do that:
python3 your_file_name.py
I hope this help you.
Sounds like you have python 2 and 3 installed and your pythonpath is pointed at python 2, so unless specified it uses that version. If you are using python I would suggest setting up a virtual environment (virtualenv) for each project, which means you could run whatever version you'd like in that project and keep all dependencies contained.
According to PEP-394,
"for the time being, all distributions should ensure that python refers to the same target as python2".
On *nix systems, there are three links to executables of python command line interpreter named
python, python2 and python3 in directory /usr/bin. The python link points to python2 according to the PEP, but you can change it to point to python3 by creating a new link to python3 and renaming it to python. Also, you have to delete the old python link.
on raspbian linux in the terminal i just run it by typing python3 file.py or just python file.py for python 2

Getting PyDev to recognize the correct Python Interpreter on Eclipse under OS X Lion

I have two versions of python installed on my mac running OSX Lion. The first is the default python version that ships with OSX and is found in /usr/bin/python. The version I want to use is the version I downloaded from python.org, and that is installed in /Library/Frameworks/Python.framework/Versions/2.7/bin/python. I want to use Eclipse and PyDev using the python.org version as the interpreter. So, in Eclipse, I go to preferences and set the version installed in /Library/Frameworks/Python.framework/Versions/2.7/bin/python to be the interpreter.
in a terminal window, if I type:
$ which python
I get "/Library/Frameworks/Python.framework/Versions/2.7/bin/python" because I set my $PATH accordingly (modified .bash_profile to permanently do so)
but if I run the following simple script in Eclipse:
import os
os.system("which python")
the script's output is "/usr/bin/python"
Things I have tried as suggested by other similar posts:
tried removing and re-adding the interpreter location
tried adding the /Library/.../package-sites to PYTHONPATH
Why isn't eclipse using the interpreter I explicitly tell it to use? Any help with this issue will be greatly appreciated!
The problem is that os.system('which python') will search for the python in the path, not the one where you're currently running (so, its output is correct).
What you want to use/check instead is sys.executable (this attribute will point to your currently running executable).
As for the wxPython issue, which error are you having? (probably another question in stackoverflow thought).
I think Eclipse is running the correct python. In your code when running under eclipse which python does not find the python running. Try
import sys
print sys.version
The issue here is that running a GUI app from the desktop/dock/folder does not load your .bash_profile and so which python does not find your change to the PATH. To change your path for GUI apps you need to edit ~/.MacOSX/environment.plist
I agree with Mark here. sys.version will be what eclipse uses to run your code. os.system("which python") will be python found in PATH that eclipse forwarded when running your code. Perhaps if you use PATH tweaks you should set environment variables for running code in Eclipse too.

Categories

Resources