Eclipse running 64-bit Python, rather than 32-bit - python

I am running OS X Lion, Python 2.7, and I am trying to setup Pygame to work with PyDev in Eclipse. I set up PyDev to use a custom-installed Python (not the default one). I forced this install to use 32-bit, which works fine in the Terminal - I can import Pygame, and other modules. However, when I use it in PyDev, it gives me a no matching architecture error. It also appears to be running in 64-bit mode.
The paths to the interpreter are the same.
import sys
print ("%x" % sys.maxsize, sys.maxsize > 2**32)
prints out ('7fffffff', False) while using Terminal, but in Eclipse/PyDev it prints out ('7fffffffffffffff', True)
The two paths (using sys.executable) are:
In Terminal it is:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
And in Eclipse it is
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
The path to the interpreter I used is: /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
I also tried manually specifying the interpreter in Terminal - Using the above path. It worked.
The default python that comes with the system is /usr/bin/python
I am using a 32-bit version of Eclipse classic as shown by an answer to this question.
Does anyone have any idea what is wrong?

You need to instruct eclipse to use 32-bit python.
Right-click on your project -> properties -> PyDen/Interpreter grammar and select "Click here to configure an interpreter not listed"
After you add your new python binary (e.g. C:\Python27\python.exe), you go back to the interpreter menu in the properties window and you select this interpreter from the corresponding drop-down menu.

I solved this by using a method describe in an answer to another question.
The answer said to go to your plugins/org.python.pydev/pysrc and open interpreterInfo.py. Then you replace all the instances of sys.executable with the path to the interpreter you want.
In my case, this meant changing them to /Library/Frameworks/Python.framework/Version/2.7/bin/python2.7
After that, open up eclipse and create a new interpreter with the same path, and it should work.

Related

Use Pycharm only as a formatter

I have used the regular IDLE python interpreter. Now I discovered PyCharm. It's a really good formatter and has a object hierarchy which I really like and auto-complete.
Unfortunately, PyCharm doesn't have the same interpreter as the regular python IDLE and I can't seem to get it to run w/ the regular python interpreter (Y'know, the one with the rocket in the icon?) and I can't seem to figure out how to get that same interpreter running
If you can give me any suggestions on how to get PyCharm to run on the regular interpreter, that would be nice, as I'm making a text based game, and the Pycharm console isn't great for those.
I've already tried looking into changing the pycharm interpreter (the rocketship python wasn't there) and the pycharm website didn't have exactly what i wanted
Basically, I want to only use PyCharm as a python formatter, and use the regular Python IDLE as a interpreter
Selecting Interpreter in Pycharm can be done by going to:
Settings -> Project -> Project interpreter
In the project interpreter dropdown, you can find interpreters you used in different Pycharm projects(if any).
If not then you can add them from the little + sign at the bottom go to the required new or existing environment and select the location of the base interpreter/ interpreter where you interpreter is saved in the file system.
As John Anderson's comment said, You can configure python interpreter by File->Settings->Progject->Project Interpreter, you said it didn't have you wanted so you must add interpreter by hand. click the setting button, and Add a python interpreter.Generally python interpreter located in C:\Users\\AppData\Local\Programs\python\python37\python.exe.

How can I change the Python version in Visual Studio Code?

These are my settings:
User Settings
{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"python.pythonPath": "python3",
"command": "python3",
}
Workspace Settings
{
"python.pythonPath": "${workspaceFolder}/env/bin/python3.6",
"git.ignoreLimitWarning": true
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "python3",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}
If I use the debug console, the version and path is right:
But the output always defaults to "python2.7", no matter what I do.
How can I fix this?
Under the view menu select 'show command pallet'. One of the commands you can then select from the command palette is 'Python: Select Interpreter'. Selecting this option will allow you to choose which version of python to use.
UPD. First, read the update #1 part in the bottom
(Try update#1 first) Looking at your screenshots I see you are using Code Runner extension. And I guess that is the way you are launching your programs.
I don't know how it works internally, but adding this line into Code-Runner extension setting.json file fixed it:
"code-runner.executorMap.python": {...
"python": "python3 -u",
...}
Found it in Code-Runner GitHub repository: https://github.com/formulahendry/vscode-code-runner/issues/366
If you type "python --help", you'll see "-u" flag stands for "unbuffered binary stdout and stderr..." - don't know why it matters here.
Update #1. This became not so convenient further - I started using python's virual environments and the solution above couldn't launch these environments, cause python3 (symlink) is always linking to the same python environment.
The solution here is to use Code-Runner's supported customized parameters, so you should change "python" line in it's settings.json to:
...
"python": "$pythonPath $fullFileName",
...
Tot's answer is what worked for me on windows 10, with a few modifications.
File -> Preferences -> Settings
Type in "python.pythonPath" in the search bar.
Change it to what you normally run python with from the command line. If you have your Path environment variable set, that's just python. If not, it's likely the full path to the executable.
This solution is for Mac and Linux:
To change your Python version from 2.7 to 3 do this:
In Vscode click on file > preferences > settings.
On the right side click on the ... (the three dots) and select (open settings.json)
In the search bar type code-runner.executorMap.
You can only change the settings on the right side.
After the last setting type a comma then
"code-runner.executorMap" and hit enter, this will copy all the settings from the default file.
Look for "python" and change the command next to it to "python3".
Save the changes and you should be good to go.
VS Code's terminal using a different python interpreter than the one you've selected
By default, it doesn't know about your interpreter, and will initialize using the default .bashrc or equivalent in the OS.
I found two relevant settings from an issue in Feb 2021.
Checking the second option Python > Terminal: Activate Environment enables automatic activation of virtual environment:
In settings.json it is called "python.terminal.activateEnvironment": true.
Update (8 Aug 2021):
Today when I opened the terminal from VS Code on Windows, it automatically inserted a line of code & C:/Users/[UserName]/[venv]/Scripts/Activate.ps1 to activate the appropriate environment associated with the selected python interpreter!
It appears the aforementioned settings is now the default behavior.
While there are changes to Terminal behavior in the release notes of July 2021 (version 1.59), I don't see virtual environment activation being explicitly mentioned.
The new behavior is documented here, in "Environments and Terminal windows".
"python.pythonPath" has been deprecated
Therefore most previous answers are outdated.
Use "python.defaultInterpreterPath" instead:
2021.6.0 (16 June 2021)
5. Added python.defaultInterpreterPath setting at workspace level when in pythonDeprecatePythonPath experiment. (#16485)
8. Show python.pythonPath deprecation prompt when in pythonDeprecatePythonPath experiment. (#16485)
2020.7.0 (16 July 2020)
9. Prompt users that we have deleted pythonPath from their workspace settings when in Deprecate PythonPath experiment. (#12533)
2020.5.0 (12 May 2020)
6. Do a one-off transfer of existing values for python.pythonPath setting to new Interpreter storage if in DeprecatePythonPath experiment. (#11052)
8. Added prompt asking users to delete python.pythonPath key from their workspace settings when in Deprecate PythonPath experiment. (#11108)
12. Rename string ${config:python.pythonPath} which is used in launch.json to refer to interpreter path set in settings, to ${config:python.interpreterPath}. (#11446)
2020.4.0 (20 April 2020)
13. Added a user setting python.defaultInterpreterPath to set up the default interpreter path when in Deprecate PythonPath experiment. (#11021)
If you wish to set a default python interpreter for all workspaces, open settings with Ctrl+Shift+P, Preferences: Open User Settings and search for Python: Default Interpreter Path. Otherwise, if you want to set it for only the current workspace, use Preferences: Open Workspace Settings instead.
In your case, you wish to set it to ${workspaceFolder}/env/bin/python3.6.
If you edit settings.json directly instead of using the GUI:
{
"python.defaultInterpreterPath": "${workspaceFolder}/env/bin/python3.6"
}
Detailed instructions can be found in the documentation "Manually specify an interpreter", including using environment variables as the interpreter's path.
Several of the answers here explain good approaches, but below are my top 2 recommendations.
Bottom Screen Navigation (ease of access)
I find this the quickest approach; however, it isn't always available for first-time users. If you're already using Python in VS Code, this is usually the easiest way to reach the Python: Select Interpreter menu. On the bottom left of your screen, look for "Python X.X.X". This is the currently detected/configured version of Python for your project, and clicking it brings you to the interpreter menu to change the Python version you're using. At the time of writing, I was using Python 3.9.1 as seen in the snippet below:
Command Palette
As #jmh denoted in his answer, you can also use the 'View' tab to navigate to the Command Palette. In the Command Palette, search for Python: Select Interpreter to bring about the same menu denoted above.
Happy coding!
In VSCode there are two paths of python:
Path that is used when you the python code using green play button up in the top right corner. This path can be set under CTRL+SHIFT+P Python: Select Interpreter.
Path that is used when you type "python" in the terminal, and this is in "Environment Variables" in Windows 10 (Similar locations under Linux and Mac). In Windows 10 you can choose to have several Python versions, usually under C:\Users\YourName\AppData\Local\Programs\Python\Python##. Just make sure you change Environment variables C:\Users\YourName\AppData\Local\Programs\Python\Python## and C:\Users\YourName\AppData\Local\Programs\Python\Python##\Scripts accordingly. This will also affect which pip you use, i.e. a pip that belongs to Python 3.8, or a pip that belongs to Python 3.9. Terminal in VSCode in general pertains to your default terminal I think. So in Windows 10 when you type "python" in CMD Line, it should be the same version as VSCode terminal.
For sanity purposes you should make sure that both "Python: Select Interpreter" and the system environment variables point to the same version of Python.
Bonus goodie in Windows 10. If you don't have environment variable setup, and you type 'python' in VSCode terminal, it'll point to C:\Users\YourName\AppData\Local\Microsoft\WindowsApps\python.exe, which just opens up python link in Windows AppStore 🙄.
Late answer really, if you find difficult to set the python version in VsCode,
If the interpreter didn't show the envname/bin/python or any desired path you want, then go to
VSCODE main page -->file-->preference-->settings
select the ... on the right corner side. You'll see USER SETTINGS, WORKSPACE SETTINGS, YOURAPP_NAME_SETTINGS. click on the your_app_name.
"python.pythonPath":
"/home/Jhon/AllWorksUbuntu/Projects/VX-350/envname/bin/python"
Play on the above to set the correct path. You're good to go!!!
Worked for me (linux user);
Assuming that you have other python versions installed in your system:
Kill the old terminal
Open a new terminal
In the new terminal instead of write "python" to select the interpreter write "python3" or "python3.8"
Looks like put only 'python' will always bring python 2.
Just a preface: VS code was working fine (Using Python 3.x) and seemingly out of the blue it started using Python 2.7. The input() function would not convert the input to a string and that's when I realized what was happening. Typing Python in the terminal window showed 2.7 was running.
For me....
Even though "python.pythonPath" was pointing to a seemingly correct location (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64), one of my environment variables was pointing to C:\Users\Mike.windows-build-tools\python27.
I deleted the environment variable and reinstalled Python 3.8 from the Microsoft Store and it immediately installed. I got a message from VS Code (it was running) that 3.8 got installed. I clicked Terminal/New Terminal, typed Python and it showed version 3.8. Typed 'Python xxx.py' and the code started working as it had been.
In my case, I checked the python version using
python --version
It showed python 2.x even though my interpreter path was 3.x. So uninstalled python 2.x from my computer through control panel. Then it worked fine for me.
Updating #Isabella answer, using current VSCode 1.65.1 and current python launcher, you can use py -version, for example py -3.8.
Thus, you can make a folder .vscode containing a single file name called settings.json containing
{
"python.defaultInterpreterPath": "py -3.8",
}
In Vscode you can go to preferences > settings, then on the right menu click on the first icon which is JSON. Look for "python.pythonPath" and "python.defaultInterpreterPath" and change the path. To find python3 path, open terminal and execute python3 dummyname. Actually you'll face an error but the point is it will show you the path!
One thing that can also be missed is the shell profile you are using in your terminal where you see the wanted python version vs the profile in your VS Code.
Hold cmd + p and write >Terminal: Select default profile
Select bash or whatever you used to install the python3 version in the terminal at the global level.
Closs the terminal and VS Code and then open it again, this time VS Code will pick the correct version
from the Lower left corner, click on the python, then vscode will navigate you to all python version that installed in your system, and select the right one for you.
Windows: Use py -3.6 --version or to create virtual environment py -3.6 -m venv venv36
Installing Anaconda, then selecting it as the interpreter is an easy way to set Python to the latest version and get the extensions that will make your Jupyter Notebook working.
For Windows Users:
I was recently faced with a similar situation where my newly downloaded python version would not show in the terminal even when I had selected the correct interpreter using 'Python: Select Interpreter'.
Here are the steps I followed that finally made it work:
Go to 'Edit the system variables' in settings.
Under the 'Advanced' tab, click the 'Environment Variables' button.
In the top half of the new window that opens, you will find 'User Variables'.
Find the path of your old python version in there.
Replace it with the path of your new python version.
Now check your python version in the VScode terminal again.

How do i know if a script is to be run with Python 2 or Python 3 [duplicate]

How do I, in the main.py module (presumably), tell Python which interpreter to use? What I mean is: if I want a particular script to use version 3 of Python to interpret the entire program, how do I do that?
Bonus: How would this affect a virtualenv? Am I right in thinking that if I create a virtualenv for my program and then tell it to use a different version of Python, then I may encounter some conflicts?
You can add a shebang line the to the top of the script:
#!/usr/bin/env python2.7
But that will only work when executing as ./my_program.py.
If you execute as python my_program.py, then the whatever Python version that which python returns will be used.
In re: to virtualenv use: virtualenv -p /usr/bin/python3.2 or whatever to set it up to use that Python executable.
Perhaps not exactly what you asked, but I find this to be useful to put at the start of my programs:
import sys
if sys.version_info[0] < 3:
raise Exception("Python 3 or a more recent version is required.")
I would use the shebang #!/usr/bin/python (first line of code) with the serial number of Python at the end
Then run the Python file as a script, e.g., ./main.py from the command line, rather than python main.py.
It is the same when you want to run Python from a Linux command line.
While the OP may be working on a nix platform this answer could help non-nix platforms. I have not experienced the shebang approach work in Microsoft Windows.
Rephrased: The shebang line answers your question of "within my script" but I believe only for Unix-like platforms. Even though it is the Unix shell, outside the script, that actually interprets the shebang line to determine which version of Python interpreter to call. I am not sure, but I believe that solution does not solve the problem for Microsoft Windows platform users.
In the Microsoft Windows world, the simplify the way to run a specific Python version, without environment variables setup specifically for each specific version of Python installed, is just by prefixing the python.exe with the path you want to run it from, such as C:\Python25\python.exe mymodule.py or D:\Python27\python.exe mymodule.py
However you would need to consider the PYTHONPATH and other PYTHON... environment variables that would point to the wrong version of Python libraries.
For example, you might run:
C:\Python2.5.2\python.exe mymodule
Yet, the environment variables may point to the wrong version as such:
PYTHONPATH = D:\Python27
PYTHONLIB = D:\Python27\lib
Loads of horrible fun!
So a non-virtualenv way, in Windows, would be to use a batch file that sets up the environment and calls a specific Python executable via prefixing the python.exe with the path it resides in. This way has additional details you'll have to manage though; such as using command line arguments for either of the "start" or "cmd.exe" command to "save and replace the "console" environment" if you want the console to stick around after the application exits.
Your question leads me to believe you have several Python modules, each expecting a certain version of Python. This might be solvable "within" the script by having a launching module which uses the subprocess module. Instead of calling mymodule.py you would call a module that calls your module; perhaps launch_mymodule.py
launch_mymodule.py
import sys
import subprocess
if sys.argv[2] == '272':
env272 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch272 = subprocess.Popen('D:\\Python272\\python.exe mymodule.py', env=env272)
if sys.argv[1] == '252'
env252 = {
'PYTHONPATH': 'blabla',
'PYTHONLIB': 'blabla', }
launch252 = subprocess.Popen('C:\\Python252\\python.exe mymodule.py', env=env252)
I have not tested this.
You can't do this within the Python program, because the shell decides which version to use if you a shebang line.
If you aren't using a shell with a shebang line and just type python myprogram.py it uses the default version unless you decide specifically which Python version when you type pythonXXX myprogram.py which version to use.
Once your Python program is running you have already decided which Python executable to use to get the program running.
virtualenv is for segregating python versions and environments, it specifically exists to eliminate conflicts.
For those using pyenv to control their virtual environments, I have found this to work in a script:
#!/home/<user>/.pyenv/versions/<virt_name>/bin/python
DO_STUFF
I had this problem and just decided to rename one of the programs from python.exe to python2.7.exe. Now I can specify on command prompt which program to run easily without introducing any scripts or changing environmental paths.
So i have two programs: python2.7 and python (the latter which is v.3.8 aka default).
While working with different versions of Python on Windows,
I am using this method to switch between versions.
I think it is better than messing with shebangs and virtualenvs
install python versions you desire
go to Environment Variables > PATH
(I assume that paths of python versions are already added to Env.Vars.>PATH)
suppress the paths of all python versions you dont want to use
(don't delete the paths, just add a suffix like "_sup")
call python from terminal
(so Windows will skip the wrong paths you changed,
and will find the python.exe at the path you did not suppressed,
and will use this version after on)
switch between versions by playing with suffixes

Python Beginner: ImportError when run from NP++ but not IDLE

I am using Python 3.3 (2.7 is also installed) and a compatible version of pygame. Recently I have been trying to switch from IDLE to Notepad++
I am using a saved shortcut in Notepad++
C:\Python33\python.bat "$(CURRENT_DIRECTORY)" "$(FILE_NAME)"
which runs the batch file:
#echo off
cd %1
%2
if not errorlevel 1 goto quit
echo.
echo.
pause
:quit
When I run C:\Python33\Foldername\imp_prob.py
import pygame
in IDLE it works fine, in Notepad++ using that shortcut it gives an ImportError: No module named pygame
My questions are:
Why is the NP++ method not producing the same result?
How can I change the shortcut or batch file to make it run stuff that IDLE can run?
What method can I use to ensure that I can import a module regardless of which directory I am running the program from?
edit: a working alternative was in the answers to How do you run a python script from within notepad++?
I had some issues with the code they provided, but replacing "python" with the full path to my python33 install solved that.
I still don't understand why pygame wouldn't import when using my run shortcut. I also don't understand why NppExec works when Run doesn't.
It sounds like you would need to set your systemvariables. Idle does not require these steps. You entered the full path to the python.exe in np++ to execute the python program, but the path to the modules etc. is still unknown.
Add the paths, and try again.
System Properties -> Advanced -> Environment Variables, in the bottom window look for a "Path" variable, Edit and append the following the existing entries (do not delete anything in there!)
;C:\Python33;C:\Python33\DLLs;C:\Python33\Lib
for Python 3.3, if you have installed it into its default directory.
To see if everything worked, open the console anywhere (shift+rightclick -> Open Command Window Here) and just type "python". The python console should open, telling you that you use python 3.3. You then also do not need to tell np++ the full python path, but instead can just use "python" again.

Eclipse / PyDev overrides #sys, cannot find Python 64bits interpreter

I'm working in a multiuser environment with the following setup:
Linux 64bits environment (users can login in to different servers).
Eclipse (IBM Eclipse RSA-RTE) 32bits. So Java VM, Eclipse and PyDev is 32bits.
Python 3 interpreter is only available for 64bits at this moment.
In the preferences for PyDev, I want to set the path to the Python interpreter like this:
/app/python/#sys/3.2.2/bin/python
In Eclipse/PyDev, #sys points to i386_linux26 even if the system actually is amd64_linux26. So if I do not explicitly write amd64_linux26 instead of #sys, PyDev will not be able to find the Python 3 interpreter which is only available for 64bits. The link works as expected outside Eclipse/PyDev, e.g. in the terminal.
Any ideas how to force Eclipse/PyDev to use the real value of #sys?
Thanks in advance!
I don't really think there's anything that can be done on the PyDev side... it seems #sys is resolved based on the kind of process you're running (not your system), so, if you use a 64 bit vm (I think) it should work...
Other than that, you may have to provide the actual path instead of using #sys...

Categories