How to SIMPLY configure VIM for Python? - python

Note: A previous question was closed because it needed to be more "focused", so I deleted the old one and modified the question so it only focus on VIM for Python.
I want to start using VIM but I´m having a hard time configuring it.
First, I did :syntax on to add some color to the syntax. Then, I wanted to change the version of Python that VIM is "verifying" as default (version 2+) to the recent version (3+) and did (something I found online) :let g:syntastic_python_python_exec = 'python3'. After I did this, the :syntax on stopped working (no more syntax highlighting) and VIM still didn´t support python3. What I mean is that if I write in Python3 I get error because it doesn't recognize version 3+
I´m using VIM (version 8+) from Terminal on Mac High Sierra.
Question:
How can I configure VIM to work with Python ? - to work with recent version of Python(3+), have syntax highlight ON and have a "file tree".
Please, answer as simply as possible so anyone who is new to VIM can understand. I searched a lot and the answers I found were old and confusing to understand for someone who is new to VIM and wants to start with it.

For the file tree, I would suggest the NerdTree plugin.
As for your syntax checker not working, I suggest you first create a Python 3 virtual environment via Pipenv, then open your vim within that virtual environment. That might solve your problem. You should always be using a virtual environment in any case if you are writing Python code.

I would suggest using coc.nvim. It is not entirely simple, but it is the simplest I know after trying a few alternatives.
Here is a relatively simple guide:
First install vim-plug: In your terminal, enter
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
Then install coc.nvim:
This plugin requires nodejs,
curl -sL install-node.now.sh/lts | bash
Now put this code in your ~/.vimrc
call plug#begin('~/.vim/plugged')
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()
Restart Vim, and type :PlugInstall and press enter. This will install the plugin for you.
Copy the default example vim configuration to the end of your ~/.vimrc (or anywhere after the plug#end()).
Inside vim, issue the command :CocInstall coc-python. This will install coc-python, which is their python support.
Inside vim, issue the command :CocConfig. This will open up a file coc-settings.json. Put in the following code in the file to use MS's Python language server.
{
"python.jediEnabled": false
}
Note that you can use jedi as well. I'm opting for MS because it's simpler to setup.
Restart vim and open a .py file. It will take a moment to install MS Python language server. After that, you should get syntax analysis and code completion.
To select an interpreter (python3), do :CocCommand, and you should see an option to set the interpreter. For virtual environments to show up, you can activate the virtual env in the command line before starting vim.

Related

VS Code can't find Python and interpreter

I searched everywhere, but did not find an answer. Since I think the answers are outdated because they use python.pythonPath and this does not exist anymore.
My problem is the following:
But I installed Python#3.10 already via homebrew (I have a mac M1 with macOS Monterey).
If I use ,,which python3'', I will get:
And here you can see the output of ,,echo $PATH'':
What should I do, so that the error message does not appear again??
You have to click on 'No Interpreter Selected' and then you can add a path to python
If nothing else will work
Go on terminal and write your python-path\python.exe python-file.y.
If this works you can create a shell script inside of your folder and then put inside the python call. Then you can start your python file with the shell script.
You can go to the Extensions Library and download the Python Interpreter for VS Code. Make sure Python is installed on your PC.
If you still have problems, try reinstalling Python and VS Code.
I think the above two answers have been well analyzed. If you can't solve the problem, you can refer to the instructions of vsocde and reinstall python.

nvim for python, ipython

I am learning python and new to nvim.
Using iterm2 is there way to send lines of code to the second tab for run with opened ipython?
As far as I know there is a plugin vim-slime but I am not sure if it is the solution to my question. Thanks.
It looks like you can do this directly.
https://coderwall.com/p/k-in2g/vim-slime-iterm2
If you run into any problems you could always run screen||tmux in iterm2 and pipe to those. Python's indentation has a slight complication in passing to the REPL. However, the git repo for vim-slime has directions on what to add to your vimrc to make this work seemlessly.

Running Python through Powershell or Git Bash

I will preface this with, I know questions like this have been asked, but I feel as I can't find my answer.
My question is this: I downloaded Python about 2 months ago to begin learning it. I just recently starting trying to use the terminal more and would like to move forward using it a lot for commits and package installs.
Unfortunately I am having a very hard time getting Python to run properly through either Git Bash or Windows Powershell. I think i may have moved files around too much before even starting with using the terminal and now I'm not sure what's correct.
Should I uninstall and re-install?
I am on Windows 10, any advice onhow to organize my files properly so everything can run correctly. I would also like to be able to open my text editor from the terminal, PyCharm or Atom.
Thanks in advance!!
Try first to simplified your PATH (as I did here) in a CMD session.
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
set GIT_HOME=C:\Path\to\Git
set PATH=%GIT_HOME%;%GIT_HOME%\bin;%GIT_HOME%\usr\bin;%PATH%
SET PATH=C:\Users\Aerovistae\AppData\Local\Programs\Python\Python36-32;%PATH%
Once you have the right path, you can replicate it in your user environment variables.
(replace C:\Users\Aerovistae\AppData\Local\Programs\Python\Python36-32 by your own Python2 installation path)
Then type bash and check python does work.

Downloading python 3 on windows

I am currently trying to figure out how to set up using python 3 on my machine (Windows 10 pro 64-bit), but I keep getting stuck.
I used the Python 3.6 downloader to install python, but whenever I try to use Command Prompt it keeps saying "'python' is not recognized as an internal or external command, operable program or batch file" as if I have not yet installed it.
Unlike answers to previous questions, I have already added ";C:\Python36" to my Path environment variable, so what am I doing wrong?
I am relatively new to python, but know how to use it on my Mac, so please let me know if I'm just fundamentally confused about something.
In environmental variables under path, add your python path... you said you already so please ensure is their comma separation between previous path..
And once added save environment variables tab. And close all command prompt then open it.
Then only command prompt will refresh with your python config..
Main thing, if you enter python which mean python 2.
For python3 type, python3 then it should work
Why are you using command prompt? I just use the python shell that comes with IDLE. It’s much simpler.
If you have to use command prompt for some reason, you’re problem is probably that you need to type in python3. Plain python is what you use for using Python 2 in the command prompt.
Thanks everyone, I ended up uninstalling and then re-downloading python, and selecting the button that says "add to environment variables." Previously, I typed the addition to Path myself, so I thought it might make a difference if I included it in the installation process instead. Then, I completely restarted my computer rather than just Command Prompt itself. I'm not sure which of these two things did it, but it works now!

How to setup Atom's script to run Python 3.x scripts? May the combination with Windows 7 Pro x64 be the issue?

I'm trying to switch from Notepad++ to Atom, but I just can't manage to get my scripts executed in Atom.
I followed this answer (so I already installed script) which is not really extensive and also the rest on the web doesn't offer anything comprehensible for beginners.
In Notepad++ NPPexec I used to
NPP_SAVE
cd "$(FULL_CURRENT_PATH)"
C:\Python34\python.exe -u "$(FULL_CURRENT_PATH)"
and in Sublime Text 2 I made it run by creating a new "Build System":
{
"cmd": ["C:\\python34\\python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
Can you please guide me how to setup Atom to be able to execute Python scripts with Python 3.4 scripts with a keyboard short-cut?
I already tried to set my init-script to:
process.env.path = ["C:\Python34\python.exe",process.env.PATH].join(";")
respectively
process.env.path = ["C:\Python34",process.env.PATH].join(";")
with no success.
When I go to Packages -> Script -> Configure Script and type
C:\\Python34\\python.exe
it works. But thats not a permanent solution.
When I press Ctrl+Shift+B to run a script, without configuring it before (as it is supposed to work), I get (suggestion of ig0774's comment implemented):
(it doesn't matter whether it is C:\Python34 or C:\Python34\)
It complains that python is not in my path - but it is.
I read multiple times that Windows 7/8 64bit together with Python 3.x could cause issues with certain packages. May this be the reason in ths case as well? I have Windows 7 Pro x64.
Update
As I've switched to VSCode and probably stay there, I'm not willing/don't have the time to try out all the answers, so I let the community judge the answers and accept always the highest voted. Please ping me, if it's not correct anymore.
This can be easily solved by editing the /home/.atom/packages/script/lib/grammars.coffee file (note that the atom folder is hidden so you might have to press ctrl+H to view hidden files and folders)
Inside grammars.coffee find:
Python:
"Selection Based":
command: "python"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python"
args: (context) -> ['-u', context.filepath]
and replace with:
Python:
"Selection Based":
command: "python3"
args: (context) -> ['-u', '-c', context.getCode()]
"File Based":
command: "python3"
args: (context) -> ['-u', context.filepath]
Save changes, restart Atom and enjoy running your scripts with python 3
EDIT: On Windows I believe the grammars.coffee file is located in
C:/Users/Your_Username/AppData/Local/atom/packages
Again, the AppData folder is hidden so you might have to change your settings to view hidden files and folders.
To expand on #matt-nona answer. You can go to his mentioned config file right from Atom. Simply go to settings then "Open Config Folder":
Then /packages/script/lib/grammars.coffee Find "Python" and make the appropriate change to python3:
Following up on Matt Nona's advice , when Atom starts-> Welcome Guide (or control+shift+T)-> 5th one down 'Hack on the Init Script'. A blank page will open and you can add that modifications in there.
Update: for any other souls looking for this answer - On my Mac I do not have a grammars.coffee file within atom script config file.
Instead, there s a grammars folder, and I have a python.coffee file in there. The same changes outlines in the screenshot (ie add '3' to the end of the two mentions of python) fixed my issue and atom automatically runs Python3 now.
Not sure if the above answers are Windows specific or if there have been dev changes since 2017.
same problem just like you. 'Packages -> Script -> Configure Script' is not permanent. So I has tryed another script runner:https://atom.io/packages/atom-runner, just found the problem is in the python script itself.
When I use atom-runner, I got error message like this:
atom-runner error
So it remind me that in the beginning of the python script: ' #!/usr/bin/env python3 '
It's obvious that the ENV_PATH is WRONG here. I should revise it in my python script.
Setting the PATH within Atom did not work, setting it with the cmd, via
set PATH=%PATH%;C:\Python34
neither, and setting it in the Windows 7 system properties failed as well.
However reinstalling Python 3.4 and check Add python.exe to Path
seems to be neccesary. Also I needed to uninstall Atom completely (inculding all packages or a least script) and reinstall it from scratch.
After all these steps:
Install Python with Add to Path
Install Atom
Install script package
it works out of the box (Ctrl+Shift+B) and no further steps are required.
I still don't know what was the reason before and I don't know which of this steps are really required. So feel free to include your procedure without reinstalling everything.
Update
Reinstalling everything is certainly not necessary, simply updating/repairing the installation with the installer is sufficient.
Use the script-runner https://atom.io/packages/script-runner/
"N.B. these keyboard shortcuts are currently being reviewed, input is welcome.
Command Mac OS X Linux/Windows
Run: Script ctrl-x alt-x
Run: Terminate ctrl-c alt-c"
And "Run Terminate" (Alt + c) to use the current python in your system.
edit your python.coffee script
$ sudo nano .atom/packages/script/lib/grammars/python.coffee
For Linux and Mac, adding environment in the script will pick correct python version. (command + I to run)
for running with python3
#!/usr/bin/env python3

Categories