PROJECTS NOTES HOME

Python development in emacs

1 [2023-12-22 Fri] nixos at work

Kind of complicated procedure because of - exited abnormally with code 1 error. As described here - https://github.com/jorgenschaefer/elpy/issues/1936

Solution:

  • add python3 in nixos home config
  • install elpy on emacs
  • pyvenv-create and name the environment "duona". It should appear in /home/s2753g/.virtualenvs/duona
  • configure where emacs will look like for a BASE(duona) virtualenv. That virtualenv must have all the python packages like jedi, autopep, yapf, black, flake8, usual stuff that you see when you do elpy-config
  • Add the line below to your elpy configuration in emacs
  • (setq elpy-rpc-virtualenv-path "home/s2753g.virtualenvs/duona/")
  • Now if you restart emacs, then do elpy-config, you will be asked if it should install the dependencies(basically all of the packages mentioned above). if you say yes - then it will say the procedure failed, because it can not download packages over work network
  • We then must source /home/s2753g/.virtualenvs/duona to activate our newly created virtualenv
  • pip install jedi autopep8 yapf black flake8 –index-url https://secret.xxxx.se/artifactory/api/pypi/pypi/simple
  • Then restart emacs and try to do elpy-config again
  • You will see that all the packages are recognized and the error will no longer be thrown

Now when we open any python file in emacs, emacs will open up with the correct virtual environment that will have all the necessary packages for us not to get errors.

If we want to create additional environments, we can do so by "pyvenv-create/workon", BUT they will not override the RPC environment, it will always stay there as a base for us not to get errors and not to uztersti our new project venv with all those yapf, flake8 and etc pakges. Great!!!

2 Dependencies

Simple python dev environment that does the job(without lsp)

Skybert paaiskinimas https://www.youtube.com/watch?v=JmmTUIahB1U&ab_channel=SkybertHacks

Maybe dont even touch LSP FOR PYTHON.

simply create environment with pyvenv-create then pyvenv-workon then install the the needed packages that are described in elpy-config pip3 install jedi autopep8 yapf black flake8 ipython importmagic

ipython - IPython is generally considered more feature-rich and provides a more interactive and convenient experience. If you prefer advanced features like tab completion, syntax highlighting, and command history, IPython may be a better choice.

set this: basically is what you get when doing ctrl+ret on a piece of code. looks more fancy, indeed, that python3 interpreter.

(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "-i --simple-prompt")

importmagic - Importmagic helps simplify the process by automatically analyzing your code and suggesting or making changes to import statements based on the modules and symbols used in your code. It can detect missing imports, remove unused imports, and reorder existing imports according to a specified style or convention.

if want auto-completion, warnings, navigation, IDE features like rename - get eglot like here - https://www.youtube.com/watch?v=JmmTUIahB1U&ab_channel=SkybertHacks

and that's it, lets get rolling.

have syntax highlights, corrections, completions, jumping to definitions etc. very nice and simple.

LSP will be using for JS development and etc.


LSP setup below(did not work)

  • LSP package and config
  • sudo apt install python3 (for python development)
  • apt install python3.10-venv (for python development)
  • Install a python language server - https://emacs-lsp.github.io/lsp-mode/page/lsp-pylsp/
  • berods dariau pip install "python-lsp-server[all]"
  • paskui sitas sudo apt install pipenv
  • create a python file
  • create a virtual environment with python3 -m venv <nameOfEnv> (or can do with pyvenv-create, then it will create virtual environment in ~/.virtualenvs folder)
  • Open emacs, do pyvenv-activate when you are inside the folder containing <nameOfEnv>
  • open terminal in emacs (shell, eshell, ansi-term, term) and do which python. You should see the <nameOfEnv> location. If you want to disable that env, do pyvenv-deactivate. Terminal will still have the <nameOfEnv> active, so might need to restart the terminal as well.
  • But then you will still get some sentinel error 1 errors
  • Lets see elpy config

more elpy docs:

[2022-02-20 Sun] TUT: "Elpy" - various python modes for easier python programming. Installs various other packages as well. A few videos to help install elpy and customize.

https://www.youtube.com/watch?v=0kuCeS-mfyc, https://www.youtube.com/watch?v=mflvdXKyA_g https://elpy.readthedocs.io/en/latest/index.html

When using tab auto completion, click f1 and get the explanation in another buffer. Company doccumentation window. and of course more amazing shortcuts Here.

If documentation suggestions get annoying - turn off eldoc mode

INSTALL:

  1. sudo pip install elpy rope jedi
  2. add export PATH=$PATH:~/.local/bin to your .bashrc file and reload Emacs.
  3. should get a message asking something about RPC, click yes.
  4. then make sure jedi is installed in your system. others use flake8, others use jedi.. idk. zamansky and the guy from he tutorial video use jedi.
  5. do M-x elpy-config to see the config
  6. check your .emacs.d folder. if there is one called "elpy" and it is empty or something, do M-x elpy-rpc-restart. Folders will appear, packages will install. Then do elpy-config
  7. pip install flake8 - get to see more syntax checks. M-x elpy-config to confirm its installed

3 Commands

lsp-doctor lsp-install server

4 virtual env

~/.virtualenvs/ - cia guli visos virtual env

  • ijunk emacs ir iskart daryk pyvenv workon
  • pip list(ar viso kompo info ziuri ar tik env environment)
  • pyvenv-workon - create a virtual env in a folder
  • pip list
  • elpy config - susiinstallink visus packages