Customizing emacs like pycharm IDE - python

I am newbie to Emacs. I mostly work in python (specifically twisted) & trying to configure it more like Pycharm IDE. I installed package elpy. But still it doesn't work well in case of auto completion. Also it shows all errors in red color either they are errors or warnings. I tweaked pyflakes to show only specific errors ( instead of showing all errors mentioned in PEP8 specifications). But I am trying to make it more like Pycharm.
Has anybody greater luck with this ? Why pycharm is so good in case of autocompletion and finding definitions/sources of functions/classes ? Also Can we configure virtualenv in emacs ?
Any suggestions/resources/ideas will be welcome.

I am a newbie myself, but I did have more luck with spacemacs, thanks to the community behind it!. And hopefully this is can be useful for those never used emacs before as well. I had hard time installing so I will make the guide very detail.
I'll be updating this if I find any more package to improve the experience
Read more at github spacemacs (Google spacemacs refactor python code)
Final Result Should Have (Check the image at the end):
Auto-complete (C-M-i)
Line number (M-x linum-mode)
Python Virtual Environment (M-x pyvenv-workon)
Syntax-checking (M-x flycheck-mode)
Warning is orange
Error is red
Show Doc of that function/class (M-?)
Table of Content:
Note
Optional Stuff
Prerequisites for Installing Spacemacs
Installation of Spacemacs
Prerequisites for Installing Python Layer
Skip to here to if you already installed spacemacs
Installation of Python Layer
Testing Python Layer
Final Note
Summary
Note:
commandline(Windows) and Terminal(Linux) as Terminal
M-x is to hold alt-key and x-key together
Optional Stuff:
Some knowledge on Vim style key-binding*
Patience reading through a lot text
Courage to start from scratch
Prerequisites for Installing Spacemacs:
Install Emacs (Version >= 24.5)
Install Git
Installation of Spacemacs:
Start Emacs and close it.
Go rename (if any) .emacs.d folder to .emacs.d.bak.
Windows: Should be in %appdata%
Linux: Should be in Home directory (Might be hidden)
Open Spacemacs website, click on install Copy the link provided.
Open up terminal, paste the link, press enter
Wait for the operation to finish
Open up emacs
By now you should see:
Something like this
Follow the Dotfile-wizard-installer
Note: that I use vim**, standard (Spacemacs), heavy and full-feature (Helm)
Get a cup of tea of your choice while spacemacs install some packages
Note: If any package fails to install, restart emacs and it should attempt to reinstall the failed packages.
Close emacs
Prerequisites for Installing Python Layer:
Python Virtual Environment
Installation of Python Layer:
Open emacs
M-x find-file and open a python file
There will be a prompt to install python layer, accept the offer
Get some cookie while it get packages for python layer
Close emacs
Open dotspacemacs file (.spacemacs) in any text editor
Go to dotspacemacs-configuration-layers and:
Uncomment auto-completion, spell-checking and syntax-checking
Uncomment mean remove ;; in front
Go to dotspacemacs-additional-packages and add the following between the brackets
flycheck-pyflakes
Pyflakes is for error checking
In terminal, run pip install pyflakes, in virtual environment of your choice
Open emacs and with for it to install some stuff
Testing Python Layer:
M-x pyvenv-workon and choose your prefered environment
M-x find-file and open a python file and make some errors
M-x linum-mode to show line number
Make a function
def asdf4ninja(): pass
Type partial function in like asdf4
It should autocomplete for you, otherwise auto-complete with anaconda C-M-i
C-M-i is hold Ctrl, Alt and character i
In the same file, import hashlib
Type in the next line hashlib. (Note the dot .)
You should see: Something like this
Note: The error checking and bottom right py3.4 is the virtual environment I am using
Final Note:
M-? to show documentation
You will need to manually
M-x pyvenv-workon to choose environment on start
M-x linum-mode to enable lines
Because I don't know how to make layers to automatically enable them
Summary:
C-M-i to auto-complete
M-? to show documentation
place the cursor in the open bracket () to see the function signature
Read more at spacemacs
Enjoy!

You can add one more thing in your list
Automatic save - (add-hook 'focus-out-hook 'save-buffer)
In case, your emacs slows down try
(global-hl-line-mode -1) in dotspacemacs/user-config

Related

Problems installing Python packages into a virtual environment in Visual Studio Code

Although I'm enjoying developing in Python in Visual Code, I'm finding managing virtual environments and packages frustrating, and particularly am struggling with installing packages in the right place. Here's my sequence of steps, and the problem I then have - I wonder if anyone could kindly tell me where I've gone wrong? Or do I really need to include the full Python path?
So first I create a new virtual environment:
I can see that this works:
I then choose to use the Python interpreter in this new virtual environment (I can't quite see why I have to do this - surely this should happen as part of the activation process - but I can live with it):
At the bottom left corner of my screen, I get the reassuring fact that I'm using the right Python interpreter:
I then install a package (I've chosen requests more or less at random):
However, this is going in my default Python location. To get it in my new virtual environment, I seem to have to include the full path to the Python interpreter:
This can't be right, although it does work - I can now see the installed package:
Can anyone help please?
Personally I haven't had luck using PowerShell (due to permissions to run PowerShell scripts) so I use Command Prompt in VS Code instead.
For PowerShell, perhaps activating your environment using Scripts\activate.ps1 will work instead. From the docs at:
https://docs.python.org/3/library/venv.html
# PS C:\> <venv>\Scripts\Activate.ps1
StackOverflowExample\Scripts\Activate.ps1
It is a bit confusing in VSCode having an interpreter selected and a different Command Prompt/Power Shell terminal used to install packages into a virtual environment.
Another confusing point is running StackOverflowExample\Scripts\activate doesn't suggest you are doing anything is wrong.
I agree with Jason Cook. activate.bat used to activate the environment in Cmd, you should take Activate.ps1 instead of activate.bat.
But you need not activate the environment by yourself. The Python extension can choose the right one to activate the environment for you when you create a new terminal.
After you select the interpreter, you need to create a new terminal. In general, we take a shortcut of Ctrl+Shift+`.
And if you want to turn off this function, you can set this in the settings.json:
"python.terminal.activateEnvironment": false,
OK, thanks to Jason/Steven I have finally got the hang of what you should be simple, but isn't. Here's what I reckon is the easiest way to create and activate a Python virtual environment in Visual Studio code. Let's say I start with the Tutorial environment active, and want to create one called ForeignHoliday (we can but dream). Start by creating the new environment in the VS Code terminal:
This creates the environment:
However, it doesn't activate it, nor does it change the default Python interpreter to use the one for the new virtual environment. You can do both of these things in one go by choosing an interpreter - click here at the bottom left of the VS Code screen:
You can now select an interpreter - your new virtual environment (irritatingly) won't be listed yet, so you'll have to find it:
Choose to find your interpreter:
Double-click on the Python interpreter in the Scripts folder in your new virtual environment (the pythonw alternative doesn't invoke a terminal window, so most people should avoid this - see this SO article):
Now press SHIFT + CTRL + ' to start a new terminal window (NOT just CTRL + ', as this switches you to an existing terminal window). You should see this:
You can now install and import packages and they will all be in the right place! I wish I'd read this answer a few days ago ...

Unable to change Python 2.7 to Python 3.8 on Sublime and VS code

I have been trying to change my python interpreter on sublime text and VS code and no matter what I do, both of them refuse to change. It always picks the default Python 2.7 version.
On sublime, I tried to create a new build tool, added the path to Python3 but nothing is working.
And on, VS code, even if I select Python3 from the drop-down menu, it doesn't change anything.
I changed the system's default python interpreter from 2.7 to 3.9 and set the path on bash profile. It fixed the issue for me.
I don't know about Sublime, but for VS Code, there looks to be quite a few things to verify:
Check your User Settings on whether you've set up a default interpreter for your applications
Are you using Window 10 WSL? Then you might want to check out issue 3227 for vscode-remote-release where the comment was:
So I was having this same exact issue until I read this article on a wsl tutorial. In there it says we should have "Remote - WSL" extension installed (already did). In the bottom left corner "Remote - WSL" installs a status button that has the "greater than" and "less than" symbols displayed. If you click that button and tell VSCode to open up in the remote WSL distro environment it fixed my issue where vscode was unable to find an interpreter for python.
For Sublime, you need to hit ⌘B with your script pane focused in order to use your new python3.sublime-build build system.
It looks like you are running your script within Terminus, which is fine, but you need to use the python3 command when running it from the command line, even if that command line is within Sublime.
Please check out my answer here on how to make a Python build system that accepts user input, as the Sublime "console" doesn't allow you to interact with your program. Since you already have Terminus installed, I'd recommend going down to the bottom of the answer and using the Terminus build system. That way, you won't have to deal with Terminal windows opening every time you run a build.
The terminal window uses your PATH, not the IDE settings, so manually typing out python --version isn't testing anything except the PATH variable
In order for the terminal to use the "correct" python binary you've specified, it would need to give the absolute path to it, which I believe using Command+B (using the build options) in Sublime would do. Similarly, you can use the Run/Debug Configurations in VSCode.
Or you can modify your ~/.zshrc to fix your PATH, for example installing pyenv to change the entire system Python version, or the one for the current folder

VSCode mouse-hover on Python Keywords not working

While beginning to teach a friend how to code in Python, I observed something in his VSCode that was different than my own - he could mouse-hover on a Python keyword such as as import or def, and it would show the Python Manual definition for the keyword. My own VSCode does not do this.
I have tried everything I can think of to find why this won't work for me, and I'm at a complete loss. I have compared my installation process to my friend's, followed the exact same steps I provided him for setting up his environment, and do not get the same results. I tried completely removing Python, VSCode and all residual VSCode files from my laptop, and reinstalling in the same order he did, still to no avail.
I followed those same steps on a different laptop at work, and I can get the MouseHover to work there. The only feasible difference I can think of is that my work machine is not using the same Windows account as both my home PC and laptop - but I do not have Settings Sync enabled so I can't see why that should matter anyway.
Any thoughts for what I'm overlooking to get this functionality working properly in VSCode for myself? I don't particularly need it, but I'd like to be in the same environment as my friend while I teach him.
We're both on VSCode 1.55.2, Python 3.9.4, using a virtual environment created from VSCode's Powershell terminal, then restarted VSCode to get it to detect the virtual environment and use it as default for Terminals, and finally pip installed and enabled Pylint as the linter.
Editing to add steps followed - I just went through all of this again on my home laptop, after completely removing all traces of VisualStudio2019 and Code, and Python. Note: this was on Windows 10, with all updates applied
Install VSCode
Install VSCode Python extension, MS version
Install Python 3.9.4
Open a new folder in VSCode, then open Terminal
python -m venv venv
create a something.py in the Explorer pane
VSCode should detect the venv and offer to load it. Say yes
VScode may also indiacte that pylint is not installed, Allow it to install, or manually pip install pylint from the venv.
in something.py type out import random
Mouse over import -- this is the discrepancy. On both of my home machines, there is no mousehover anything. on both of my friend's machines, as well as my machine at work, the following is shown:
With help from riov8's comment on the original post, I was able to compare the Experiment Groups that were in effect for each of the environments.
In VSCode, with a Python file loaded, switch to the Output panel (in same area as Terminal) and change the dropdown to Python.
Scroll to the top of this panel, and examine the Experiment Groups you belong to.
Most of the experiment groups do not apply to this issue.
Look specifically for either pythonJediLSP, pythonJediLSPcf or if
you have neither.
If you are experiencing the issue I describe, then at the bottom of this same panel, when you first mouse over a python keyword after starting VSCode, you will get errors similar to the following:
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the line is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
%UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Providing the column is now done in the functions themselves like `Script(...).complete(line, column)`
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:598: DeprecationWarning: Deprecated since version 0.17.0. Use the project API instead, which means Script(project=Project(dir, sys_path=sys_path)) instead.
sys_path=sys.path,
Error 2021-04-16 15:33:40: stderr jediProxy Error (stderr) %UserProfile%\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\completion.py:626: DeprecationWarning: Deprecated since version 0.16.0. Use Script(...).infer instead.
script.goto_definitions(), request["id"]
The following scenarios resolved the issue for me:
User belongs to experiment group pythonJediLSPcf:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python> Experiments: Opt Out From, click Edit in settings.json
in the section "python.experiments.optOutFrom", add "pythonJediLSPcf" (quoted)
if it does not exist, add another section "python.experiments.optInto"
In the OptInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User does not belong to either pythonJediLSPcf or pythonJediLSP:
File > Preferences > Settings
in Search Settings, begin typing python.experiment
under Python › Experiments: Opt Into, click Edit in settings.json
In the "python.experiments.optInto section, add "pythonJediLSP" (quoted)
Save the settings.json, and restart VSCode.
MouseHover tooltips on Python keywords now should work.
User belongs to pythonJediLSP:
It was my experience that this was the experiment required in order for the feature to work properly. If you have this experiment enabled already, then ensure you do not also have pythonJediLSPcf. Disable it if you do, following the instructions in the first sub-heading. If it continues to not work, then there may be another conflict that I did not experience myself.
I am posting a GitHub issue regarding this problem and will update this answer with the link when done.

Pycharm is not showing Pygame properties [duplicate]

I want to configure Pylint as an external tool in my entire project directory for a Python project that I'm working on. I've tried to use the repository as a module with __init__.py and without, and it's not working either way.
I'm having difficulty setting up Pylint to run with PyCharm. I know that I should be running it as an external tool, however the settings confuse me.
The authoritative source on their documentation is broken, so I can't check that up either.
You can set up Pylint to work with PyCharm by following the following steps:
Install pylint:
$ pip install pylint
Locate your pylint installation folder:
$ which pylint # MacOS/Linux
/usr/local/bin/pylint # This is just a possible output - check yours
<!-->
$ where pylint # Windows
%LocalAppData%\Programs\Python\Python36-32\Scripts\pylint.exe # Possible location
Open the PyCharm settings window with menu File → Settings, then navigate to menu Tools → External Tools in the sidebar. (Or search "external tools")
Set up an external tool by clicking on the + sign and filling in the fields accordingly. In Program use the path you got when running which pylint. For the other values, you can use the same from the image.
Run pylint from menu Tools → External Tools → pylint:
Look at your output in the PyCharm terminal
For more details, refer to Pylinting with PyCharm.
If you want to use Pylint to check your whole project or a particular file or directory, you can right click on your project root, file or directory, then activate External Tools → pylint as shown below.
Because I didn't find a working ready-made setup, these are the settings I use in PyCharm CE 2018.1 on macOS:
1 - pip install pylint in your project virtualenv or globally
2 - Add new external tool and configure:
Program: pylint
Arguments: "--msg-template='{abspath}:{line:5d},{column:2d}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\,\s*$COLUMN$:
Notice the required double quotes for the msg-template, and the escape chars for the output filters. The output filter allows to click on the file path and open the location in the IDE source editor.
Only missing feature would be the output filters to plot the lint descriptions directly into the source view, as is done with the builtin linter. No such feature at this time though.
You can try this Pylint PyCharm plugin:
It provides both real-time and on-demand scanning of Python files with Pylint from within PyCharm/IntelliJ IDEA.
Once you have it installed, the real-time inspection works automatically.
For the on-demand you have several options that go from just checking the current open file to scan the entire project:
(Just for the sake of transparency, I am the developer.)
I now use and recommend the PyCharm plugin which didn't exist when I first wrote this answer.**
A colleague pointed me towards pylint-pycharm on GitHub. It's a wrapper around Pylint with output formatted for PyCharm.
Here's how I set it up:
git clone https://github.com/perses76/pylint-pycharm.git
cd pylint-pycharm
python setup.py build
This creates build/scripts-2.7/pylint-pycharm
Then, in PyCharm, create a new External Tool with these settings:
Program: path to your installation of pylint-pycharm
Arguments: --virtualenv=$PyInterpreterDirectory$/.. $FileName$
Working directory: $FileDir$
Output filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*
Now run it from menu Tools* → External Tools → PyLintPyCharm. Each line of output will be hyperlinked to the relevant position in the source code.
A note on the previous answers.
I was searching for a method to make PyCharm aware of the output syntax so I can directly jump to the file locations. That works without using additional tools.
Pylint can be configured to output messages in a specific format using the msg-template option in the pylintrc file or the CLI option --msg-template.
I set it to:
msg-template='{abspath}:{line}:{column}: {msg_id} {msg}'
In the External Tools settings, the Output filters: can be set to
$FILE_PATH$:$LINE$:$COLUMN$: .*
so PyCharm shows links to directly jump to the reported locations.
This can be combined with output-format=colorized so I get this:
PyCharm does not recognize the column despite having it configured. But having the file and line is enough for me.
At first install Pylint with pip:
pip install pylint
You have to open “Settings → Tools → External Tools“ and press the “+” button at PyCharm.
Here are an example with good settings.
Roberto Leinardi has created a Pylint plugin for PyCharm which really works and integrates well into the IDE:
Easy to install from the repositories, full instructions under:
pylint-pycharm
I have a short, yet a happy, experience with it so far! Kudos to Roberto.
Thanks to information here, and updated documentation from PyCharm, I've been able to get this to work nicely to also use the virtual environment for the project (ensuring that any packages can be deployed within the virtual environment and do not need to be deployed globally).
Taking what lkraider provided earlier, but with slight modifications:
Ensure you install Pylint within the virtual environment. Note, make sure that when you created the virtual environment you did not select "Inherit global site-packages". If you do then Pylint will end up being globally and this will not work.
Add a new external tool and configure. This is slightly different compared to what lkraider provided. For one I wanted it to look more like normal Pylint output, hence my msg-template (and Output filter) is a bit different. Each to their own.
The second change is the more critical one for executing Pylint based on the virtual environment. That is the program parameter where I use $PyInterpreterDirectory$.
Program: $PyInterpreterDirectory$/pylint
Arguments: "--msg-template='{abspath}:{line:5d}:{column}: {msg_id}: {msg} ({symbol})'" --output-format=colorized "$FilePath$"
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$:\s*$LINE$\:\s*$COLUMN$:
External tool for Pylint

PyCharm not recognizing my python interpreter and PIP packages. Maybe PATH issue?

So I have been having issues with getting PyCharm to find my python3 interpreter on my Mac. I have downloaded a few packages with pip from the terminal and when I use the python shell from the terminal and import them, I get no errors.
When I check where these packages are downloaded, I get this path:
/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.>7/lib/python3.7/site-packages
So the issue is when I try to use the PyCharm IDE I don't have access to these packages. I have gone into the settings to change the interpreter and I have tried all these options, none of which show my installed packages.
![1]:https://i.imgur.com/dhTg8CV.png
I have also tried navigating to that path where it shows my packages are downloaded and selecting the python.exe (the highlighted file) I find, but get this error with PyCharm ->
![2]:https://i.imgur.com/DbW5y6I.png
![3]:https://i.imgur.com/B8H4hlA.png
I have a feeling I am selecting the wrong python.exe but I don't know where to look. I also think the problem could stem from me installing iTerm and the zshrc bash program and it changed my paths, but I don't know why python would be working from my terminal then. Just in case, this is what my Path is in my zshrc file:
export PATH=$HOME/bin:/usr/local/bin:$PATH
export PATH="$HOME/.composer/vendor/bin:$PATH"
export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"
Sorry for the long post and no inline images, this is my first post. Any help would be greatly appreciated! Just trying to get this to work so I can work on a project.
Update
just thought it would be useful to know that when i type "which python3" in the terminal it returns -> /usr/local/bin/python3
The simple solution is
Open the pycharm
Go to top left most side on IDE and open FILE dropdown.
Open the project which contains PIP files.
By this procedure pycharm automatic detect the virtual environment.

Categories

Resources