How can I configure Geany to compile and run my Python programs? - python

Under the Build menu, I can see 'Execute' option, but it is greyed out.
The only option available is 'Set Includes and Arguments'. When I click that both fields are already filled out. What do I have to write there?

I don't need to configure anything in Geany. I just hit F5 and the current module is executed.
Are you sure that your file is recognized as a Python source file? Which version of Geany are you using (I am using the SVN version, which is pretty stable; damn, it's rock solid stable ;-))? I have a slightly more developed configuration for Python compilation in my version, but the commands are the same and it works very well.

I had the same problem and found the following solution:
In the 'Build Options' dialogue, click on '_Execute' at the bottom of the dialogue and the _execute options become editable.
I put the full path name to my Python install in there (C:\Python27\python %F) or (C:\Python32\python %F) and it seems to work just fine.

For some unknown reason a new installed Geany at Ubuntu 19.04 (Disco Dingo) had python "%f" at Build → Set Build Commands → Execute default settings.
I solved a similar issue with python3 "%f" instead.

Also don’t forget to set the “KeyBinding”, i.e., the keyboard shortcut-key (single or combination) you press to activate “Run, Execute, Compile, Save, Find, Print, etc...
To do so;
Menu Edit → Preferences → Keyboard Shortcuts.
Here you can choose/change any Key/s to map to any action you want.
Why? Because some other Linux program might have been overridden, or might have taken the default Geany assigned map keys.

You need to set the path variables in the beginning. Or you need to set the variables in Build → set build commands.
This video can give you good clarity:
Install Geany for Python
Your compile path is looking fine. But in the execution path, give the full path of your python.exe. In my case it is...
C:\python27\python "%f"

Find the Python console path.
Now set the environment variable

Related

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.

NppExec custom shortcuts not working

I decided to use NppExec to run my Python files from notepad++, however I encountered a problem. I created a simple script with one line:
python -i $(FULL_CURRENT_PATH)
and added the script to the "Menu items" in nppExec advanced options. The script appears in my macros tab and does what I need when I run it. The problem is I am unable to set a keyboard shortcut to this command. The command appears in my shortcut mapper (under plugin commands) and I can set it to
shift+ctrl+p
but the problem is that it simply does not work when I hit the three keys. The key combination is not the problem, as I was able to create a test macro that ran when I hit shift+ctrl+p.
Edit:
More updates: the shortcuts.xml in APPDATA does change when I add the shortcut, the line:
<PluginCommand moduleName="NppExec.dll" internalID="20" Ctrl="yes" Alt="no" Shift="yes" Key="80" />
is added. This is exactly my ctrl+shift+p command, it's there, it just does not fire.
Is anyone else experiencing this strange behaviour?
Any help will be greatly appretiated.
Ctrl+Shift+P is by default assigned to Playback command. Notepad++ is buggy when it comes to shortcut mapping, can easily create collisions. See for example open bug #3635.
My suggestion is to try to assign it to something else (personal preference is F5) and double check that it's not already assigned!

Running a module from the pycharm console

I'm new to python and pycharm and I'd like to run a module from the pycharm console in the same way as you can from IDLE, if it's possible.
The idea is to create simple functions and test them "live" using the console.
...how do you do that in pycharm?
Running python scripts using pycharm is pretty straightforward, quote from docs:
To run a script with a temporary run/debug configuration Open the
desired script in the editor, or select it in the Project tool window.
Choose Run on the context menu, or press Ctrl+Shift+F10. So
doing, a temporary run/debug configuration is created on-the-fly.
Besides there is a "Python Console" available in pycharm: see documentation.
UPD:
Here's an example.
Imagine you have a python module called test_module.py:
def a(*args, **kwargs):
print "I'm function a"
def b(*args, **kwargs):
print "I'm function b"
Then, in pycharm's "Python Console" you can do this:
>>> from test_module import *
>>> a()
I'm function a
>>> b()
I'm function b
If you need to execute a part of an existing code, you can use the Execute Selection in Console feature: select the code snippet -> right click -> "Execute Selection in Console".
For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.
Edit: To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)
Right Click --> Run File In Console
Done!
Looks like in version 2018.3, this option is now Run with Python console in Run/Debug Configurations:
What you're looking for is the feature called Execute Selection in Console which is described in section Loading Code from Editor Into Console of PyCharm's online help.
Select the script lines that you want to execute and press Shift+Alt+E
You can run the Find Action shortcut (Ctrl+Shift+A or ⌘+⇧+A on mac), then type run file, and choose the option Run file in Console.
In pycharm do:
Run>Edit Configuration>Show command line afterwards
Assuming your code is in file MySimpleCode.py you can simply say
run MySimpleCode
in the PyCharm console. This assumes that you have set your working directory properly; e.g. if MySimpleCode.py is located in d:\work on a Windows system you must execute
cd d:\work
first. In my opinion the other solutions miss what the post really wants: simply executing a file like from a DOS or Unix shell, or a .m script in MATLAB. No messing with imports, projects and so on. If you use CTRL SHIFT F10 your code gets executed, sure, but in a different environment, so you have no access to variables created in your code. I assume the question means that you want to do further work with the results of the script.
Explanation for people with MATLAB background: In most Python IDEs you have to configure an interpreter first in some kind of project. The MATLAB equivalent would be a master IDE where you can choose your MATLAB version for each project. This makes it possible to run your Python code on the CPU, your GPU or even an external NVIDIA board with different settings (after several days in the installation hell). For the beginner this is very confusing, because for simple code samples any "default" interpreter should suffice. Unfortunately this is not the case for Python (2 or 3? 2.x or 2.y? which package version?), and it will get worse as you progress (which 32 or 64 bit version of TensorFlow is available for Python 3.x? and so on).

Using Python 3.1 with TextMate

TextMate seems to use the built-in Python version I assume (sys.path doesn't work). How do you configure it to use 3.1 instead? I've already installed the 3.1 package and I can use IDLE for interactive sessions, but I need to use TextMate now.
Thanks
TextMate uses the value of the TM_PYTHON variable to find the path to the Python interpreter. A good solution is to take advantage of TextMate's ability to define variables like TM_PYTHON on a per-project basis:
Open a new or existing TextMate Project (File -> New Project or File -> Open)
De-select any file in the project list sidebar.
Click on the Get Info (i) icon in the sidebar. A Project Information pane appears.
Click + to add a new variable.
Enter TM_PYTHON in the Variable field and the full path to the desired python in the Value field (for example, /usr/local/bin/python3.1).
Close the Information window and save the Project (File -> Save Project As).
Then you can add files as needed to the project and they will be run under the chosen python with TextMate Python bundle's Run Script command. You might want to save a Python 3 project, say, for running ad-hoc scripts under Python 3. For bigger projects, you'll want to create a separate TextMate project for it anyway.
To change the Python version used globally within TextMate:
From the TextMate menu bar, open TextMate -> Preferences
click on the Advanced pane
click on the Shell Variable tab
click the + to add a new variable
enter TM_PYTHON in the Variable field and the full path to the python in the Value field (perhaps /usr/local/bin/python3.1)
As Alex points out, you may break other TextMate functionality by changing the Python version globally so the per-project change is probably a better solution.
UPDATE (2010-10-31):
There is another approach that may be easier to use for some projects. The Run command in TextMate's Python bundle appears to respect a shebang line in the file being run. So, instead of modifying TM_PYTHON, you can specify the path to the interpreter to be used by including a first line like this:
#!/usr/local/bin/python3.1
# sample code to show version
import sys
print(sys.version_info)
In many case you would prefer not to hardwire the absolute path but manage use through the normal shell PATH environment variable. Traditionally /usr/bin/env is used for that purpose. However when running under TextMate, your shell profile files are not normally used so any changes to PATH do not show up there including possibly /usr/local/bin or /opt/local/bin or wherever your desired python3 command is located. To get around that, you can add or modify a global PATH shell variable to TextMate -> Preferences (see above) with a value of, say, /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin. Then you can use a more general shebang line like this:
#!/usr/bin/env python3
(This all seems to work with the most recent vanilla TextMate and its Python bundle: no guarantees about earlier versions or with other Python bundles.)
Late to the party, sorry! I take it you want to run the script using TextMate's 'built-in' interpreter? I've found the simplest solution is to add a shebang, which works extremely well;
#!/usr/bin/env python3
for python 3.1 or;
#!/usr/bin/env python
for default system python, although the latter is superfluous for the exercise.
According to this long thread (which was about Python 3.0, and the TextMate version existing back last spring, but I believe is still valid for Python 3.1 and today's TextMate), you can get it done (e.g. via #Ned's answer), but once you do many TextMate commands may well break (because they're written to use Python 2, and Python 3 is not backwards compatible with Python 2 -- for example, the use of reload, which disappeared in Python 3, is repeatedly mentioned in the thread). Still, it might work if you do not use or need much of TextMate's functionality (LaTeX typesetting for example was mentioned as something that totally breaks once you make TextMate use Python 3 instead of Python 2).
the shebang is the best solution, to see where python 3 is installed type in terminal:
which python3
you will get something like this:
/usr/local/bin/python3
if nothing shows up first install python3
and at the top of your script insert:
#!/usr/local/bin/python3
I wanted to achieve this in TextMate version 2.0.23 with the aim to use python3 as the default.
So this is how I could set the TM_PYTHON variable (based on #Ned Deily's answer above):
Open Textmate
Hit CMD + ; to open the settings
Now Add a new Variable with the + sign
4. In the Terminal.app I typed which python3 which gave me /usr/local/bin/python3.
Now whenever I open a python script and hit CMD + R it will execute as a pyhton3 script by default while "honouring" the installed PIP packages.

How to start IDLE (Python editor) without using the shortcut on Windows Vista?

I'm trying to teach Komodo to fire up IDLE when I hit the right keystrokes. I can use the exact path of the shortcut in start menu in the Windows Explorer location bar to launch IDLE so I was hoping Komodo would be able to use it as well. But, giving this path to Komodo causes it to say that 1 is returned. This appears to be a failure as IDLE doesn't start up.
I thought I'd avoid the shortcut and just use the exact path. I go to the start menu, find the shortcut for IDLE, right click to look at the properties. The target is grayed out, but says "Python 2.5.2". The "Start in" is set to, "C:\Python25\". The "Open File Location" button is also grayed out.
How do I find out where this shortcut is really pointing? I have tried starting python.exe and pythonw.exe both in C:\Python25, but neither starts up IDLE.
There's a file called idle.py in your Python installation directory in Lib\idlelib\idle.py.
If you run that file with Python, then IDLE should start.
c:\Python25\pythonw.exe c:\Python25\Lib\idlelib\idle.py
In Python 3.2.2, I found \Python32\Lib\idlelib\idle.bat which was useful because it would let me open python files supplied as args in IDLE.
Here's another path you can use. I'm not sure if this is part of the standard distribution or if the file is automatically created on first use of the IDLE.
C:\Python25\Lib\idlelib\idle.pyw
If you just have a Python shell running, type:
import idlelib.PyShell
idlelib.PyShell.main()
there is a .bat script to start it (python 2.7).
c:\Python27\Lib\idlelib\idle.bat
Python installation folder > Lib > idlelib > idle.pyw
Double click on it and you're good to go.
You can also assign hotkeys to Windows shortcuts directly (at least in Windows 95 you could, I haven't checked again since then, but I think the option should be still there ^_^).
The idle shortcut is an "Advertised Shortcut" which breaks certain features like the "find target" button. Google for more info.
You can view the link with a hex editor or download LNK Parser to see where it points to.
In my case it runs:
..\..\..\..\..\Python27\pythonw.exe "C:\Python27\Lib\idlelib\idle.pyw"
I setup a short cut (using windows) and set the target to
C:\Python36\pythonw.exe c:/python36/Lib/idlelib/idle.py
works great
Also found this works
with open('FILE.py') as f:
exec(f.read())
Another option for Windows that will automatically use the most recent version of Python installed, and also doesn't make you look for the installation path:
Target: pyw -m idlelib
Start in: Wherever you want
I got a shortcut for Idle (Python GUI).
Click on Window icon at the bottom left or use Window Key (only Python 2), you will see Idle (Python GUI) icon
Right click on the icon then more
Open File Location
A new window will appears, and you will see the shortcut of Idle (Python GUI)
Right click, hold down and pull out to desktop to create a shortcut of Python GUI on desktop.
Python installation folder > Lib > idlelib > idle.pyw
send a shortcut to desktop.
From the desktop shortcut you can add it to taskbar too for quickaccess.
Hope this helps.
If it's installed on windows 10 without changing default location, it seem it is in "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.1776.0x64__{BUNCHOFRANDOMSTRINGS}"
and you won't be able to open it.
Good luck finding how open .py by default with idle.

Categories