Emacs Python Mode and Executing commands - emacs

I have emacs24 installed on windows in C:\Program Files(x64) and python2.7 under C:\Python27
I've tried to install python mode to send commands to the interpreter from emacs. My init.el file in Users/myname/.emacs.d:
(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.1")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)
(setq py-shell-name "C:\Python27\python.exe")
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(py-shell-name "C:\\Python27\\python.exe"))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
I've put the python mode files in: C:\Users\myname\.emacs.d\python-mode.el-6.1.1
An interactive python session can be formed in a buffer by choosing the PyShell > Default Intepreter option in the menu, but if I open a .py file and try to run a hellow world like by going to the menu PyExec > Execute Statement I get errors of this nature:
Opening output file: no such file or directory, c:/Users/Ben/AppData/Local/Temp/C-/Python27/python.exe-705218Y.py
How can I set up so I can edit python code and then send lines to the python interpreter in another buffer without this error?

You are using setq and custom-set-variables to set py-shell-name. If I remember correctly, custom-set-variables does not work if you use setq before it. Also, you need to escape backslash when you write it in string literals. Using one of the following should solve your problem.
To use setq, fix backslashes:
(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.1")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)
(setq py-shell-name "C:\\Python27\\python.exe")
To use custom-set-variables, just remove setq:
(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.1")
(add-to-list 'load-path py-install-directory)
(require 'python-mode)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(py-shell-name "C:\\Python27\\python.exe"))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)

Related

Setting up emacs on new machine with init.el and package installation

Like presumably many emacs users I have my own emacs config file ~/.emacs.d/init.el for configuring emacs the way I like. So when I start using a new machine I copy my emacs config file to it. Now, the problem is that my emacs config file depends on a few packages that I have installed via the emacs package manager, but due to the missing packages I'm unable to successfully install packages.
I can of course start emacs without my config file (emacs -q), but then the problem is that only the default repo is available, so I cannot actually install the package I need to install in order to successfully start emacs with my config file.
So what I have usually done is to temporarily comment out stuff in my emacs config file, so that I'm able to successfully install the packages, and then I can uncomment it and restart emacs with my full config. But this is cumbersome, and usually takes a few tries before I comment out all the needed stuff. Surely there must be a better way that I'm missing?
What you can do is to declare packages you use. Then add some code that runs every time you open Emacs. It checks each package from that list if it has been installed or not. When it's not, it installs it.
A quick example from my config file:
;; first, declare repositories
(setq package-archives
'(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.org/packages/")))
;; Init the package facility
(require 'package)
(package-initialize)
;; (package-refresh-contents) ;; this line is commented
;; since refreshing packages is time-consuming and should be done on demand
;; Declare packages
(setq my-packages
'(cider
projectile
clojure-mode
expand-region
helm
jinja2-mode
magit
markdown-mode
paredit
wrap-region
yaml-mode
json-mode))
;; Iterate on packages and install missing ones
(dolist (pkg my-packages)
(unless (package-installed-p pkg)
(package-install pkg)))
And you're good.
You can place the initialization elisp that installs the packages you need in a separate file, then start emacs -q, then load and evaluate the packages elisp file.
I'm using the following code, in a file of its own, to handle packages. This code also defines the packages I'm using and allows for dynamically adding and loading packages.
If you load this file first thing from your init.el, then you would probably be able to just start Emacs as usual, and missing required packages will be installed automatically.
Update
I was somewhat bothered with the way I used defvar to define the package list variable. I've done some reading and fixed the code below—now it defvar a variable my-packages-package-list and then setq it to the list of packages to install. As far as I understand, this is a more idiomatic way of defining and using variables. As a result, this code now byte-compiled without any warnings.
For those who are interested, some information of using defvar and setq may be found here and in the Emacs` manual.
(require 'package)
(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
;; ("marmalade" . "https://marmalade-repo.org/packages/")
("melpa" . "https://melpa.org/packages/")
("org" . "https://orgmode.org/elpa/")))
(setq package-archive-priorities '(("melpa" . 10)
("gnu" . 5)
("org" . 2)
;; ("marmalade" . 0)
))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
;; the following code will install packages listed in myPackages if
;; they are not already installed
;; https://realpython.com/emacs-the-best-python-editor/
(defvar my-packages-package-list "List of custom packages to install.")
;;; this allows for dynamically update and install packages while
;;; Emacs is running, by modifying this list, and then evaluating it
;;; and tha mapc expression below it
(setq my-packages-package-list
'(;; add the ein package (Emacs ipython notebook)
ein
;; python development environment
elpy
;; beutify python code
py-autopep8
;; git emacs interface
magit
;; debuggers front end
realgud
;; multiple major mode for web editing
;; multi-web-mode
;; major mode for editing web templates
web-mode
;; docker modes
docker-compose-mode
dockerfile-mode
;; list library for emacs
dash
;; collection of useful combinators for emacs lisp
dash-functional
;; major modes for yaml
yaml-mode
;; major modes for markdown
markdown-mode
;; major modes for lua
lua-mode
;; major modes for fvwm config files
fvwm-mode
;; treat undo history as a tree
undo-tree
;; flychek
;; flychek-clojure
;; flychek-pycheckers
;; Clojure for the brave and true - below; amit - some packages
;; commented out by me until I'll be sure they are needed
;; makes handling lisp expressions much, much easier
;; Cheatsheet: http://www.emacswiki.org/emacs/PareditCheatsheet
paredit
;; key bindings and code colorization for Clojure
;; https://github.com/clojure-emacs/clojure-mode
clojure-mode
;; extra syntax highlighting for clojure
clojure-mode-extra-font-locking
;; integration with a Clojure REPL
;; https://github.com/clojure-emacs/cider
cider
;; allow ido usage in as many contexts as possible. see
;; customizations/navigation.el line 23 for a description
;; of ido
;; ido-ubiquitous
;; Enhances M-x to allow easier execution of commands. Provides
;; a filterable list of possible commands in the minibuffer
;; http://www.emacswiki.org/emacs/Smex
;; smex
;; project navigation
;; projectile
;; colorful parenthesis matching
rainbow-delimiters
;; solarized theme
solarized-theme
;; edit html tags like sexps
;; tagedit
;; help finding keys
which-key
;; xkcd
xkcd
;; Clojure exercises
4clojure
))
(mapc #'(lambda (package)
(unless (package-installed-p package)
(package-install package)))
my-packages-package-list)

How do you "import" the github MELPA fetcher?

I get Symbol's value as variable is void: github when I have the following in my .emacs file (the error comes from the last expression):
(require 'package)
(add-to-list 'package-archives
'("MELPA Stable" . "http://stable.melpa.org/packages/") t)
(add-to-list 'package-archives
'("gnu" . "http://elpa.gnu.org/packages/") t )
(package-initialize)
(package-refresh-contents)
(package-install 'flycheck)
(global-flycheck-mode)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages (quote (haskell-mode idris-mode flycheck))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(add-hook 'after-init-hook #'global-flycheck-mode)
(idris-mode
:repo "idris-hackers/idris-mode"
:fetcher github
:files (:defaults "logo-small.png"))
It looks like the last expression in your .emacs file comes from the MELPA recipe for idris-mode. You don't need that in your init file - just type M-x list-packages, find idris-mode in the list, and install it by typing i x. After that, idris-mode will be available every time you start Emacs.

Installing auto-complete in emacs

I am a new to Emacs , i downloaded auto-complete , moved it to ~/.emacs.d/plugins/ then modified the .emacs file to look like :
(add-to-list 'load-path (file-name-as-directory
(expand-file-name "~/.emacs.d/plugins/auto-complete"))\
)
(require 'auto-complete)
(global-auto-complete-mode t)
(ac-config-default)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(menu-bar-mode nil)
'(package-archives (quote (("melpa" . "http://stable.melpa.org/packages/") ("g\
nu" . "http://elpa.gnu.org/packages/")))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
when i restart emacs , it displays the following error :
Warning (initialization): An error occurred while loading `/Users/zeaksilva/.emacs':
File error: Cannot open load file, auto-complete
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.
Put (setq debug-on-error t) at the beginning of your init file. Or better yet, append --debug-init to the command line you use to invoke Emacs. That will open the debugger when the error occurs. But it seems that the file that has (provide 'autocomplete) in it, or that file's directory, is not in your load-path. When the debugger opens, use C-h v load-path, and see whether it is correct.
And try commenting out all of the extra stuff in your init file, which does not seem related to this problem: the custom-set-variables and custom-set-faces. IOW, simplify the sack of stuff that you try to debug.

Emacs: can't autostart projectile installed through MELPA

I'm fairly new to emacs. In fact I'm learning the editor and trying to setup something that will replicate "go to a file inside the project" feature known from Code::Blocks or certain plugins of notepad++.
'projectile' fulfills this need, and I installed it through MELPA. Package installed properly, as I can start it with M-x projectile-global-mode and C-c p commands are recognized.
However, if I put it into my .emacs file, Emacs starts with an error:
Symbol's function definition is void: projectile-global-mode
Contents of my .emacs file are as follows:
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(global-whitespace-mode 1)
(global-linum-mode 1)
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(projectile-global-mode 1)
When I try to (require 'projectile) first, I only end up with another error:
'File error: Cannot open load file, projectile'
I'm using Emacs 24.3.1.
How do I put this on autostart properly?
By default, Emacs initializes packages after evaluated init.el. Hence, in a standard setup, packages are not yet available while init is evaluated.
Use (add-hook 'after-init-hook #'projectile-global-mode) to enable Projectile only after packages are initialized, or explicitly initialize packages at the beginning of your init.el with the following code:
(require 'package)
(setq package-enable-at-startup nil) ; To avoid initializing twice
(package-initialize)
You have to load projectile first, e.g. by using this:
(require 'projectile)
(projectile-global-mode)
you can add
'(initial-major-mode (quote projectile-global-mode))
to your .emacs(or init.el or whatever your file is called) file in the custom-set-variable section.
Alternatively, in newer versions of emacs, the menu Options | Customize Emacs | Specific Option you can type 'initial-major-mode' and this will take you to an interface where emacs can customize itself with that setting. just remember to apply and save

Emacs: Where to put the psvn.el file?

I am totally new to emacs and is starting to learn how to use it effectively.
The first thing I wanna use is the svn mode.
I downloaded psvn.el and put it in the ~/.emacs.d directory
Then following the instruction in the comment part of the psvn.el file, I put this line
(require 'psvn)
Into the .emacs file
This is my current .emacs file
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(inhibit-startup-screen t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(require 'psvn)
Now when I starts emacs, I got this error message:
An error has occurred while loading `/home/akong/.emacs':
File error: "Cannot open load file", "psvn"
To ensure normal operation, you should investigate the cause
of the error in your initialization file and remove it. Start
Emacs with the `--debug-init' option to view a complete error
backtrace
Did I put the psvn.el in a wrong location?
I am using cygwin + WinXP
This is because Emacs cannot find any file providing psvn on its load-path.
In your shell:
mkdir -p ~/.emacs.d # Make the directory unless it exists
mv /some/path/psvn.el ~/.emacs.d/ # Move psvn.el into that directory
In your Emacs init file (often ~/.emacs):
(add-to-list 'load-path "~/.emacs.d") ; Add this directory to Emacs' load path
(require 'psvn) ; Load psvn
EDIT: I just realized that you are on Windows XP. I'm not sure how Cygwin will handle all of this, but the procedure is pretty much the same outside of Cygwin, just remember that ~ is %APPDATA% on Windows XP, so .emacs.d and .emacs should both be in that directory.
I guess you have problem finding your home directory on Windows? Try C-x d ~ RETURN (run dired on your home directory) to see where you home directory is, then do what the other answers say: put psvn.el in .emacs.d and add ~/.emacs.d in your load-path
First thing you're going to want to do is add .emacs.d to your load path so it knows where to look. Generally most people store .el plugins in ~/.emacs.d/site-lisp so i do this:
;; >>> Configure Load Path <<< ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq emacs-config-path "~/.emacs.d/")
(setq base-lisp-path "~/.emacs.d/site-lisp/")
(setq site-lisp-path (concat emacs-config-path "/site-lisp"))
(defun add-path (p)
(add-to-list 'load-path (concat base-lisp-path p)))
;; I should really just do this recursively.
(add-path "")
;; (add-path "some-nested-folder")
Now (require 'psvn) should work out fine.