Getting pdb in Emacs to use Python process from current virtualenv - python

I am debugging some python code in emacs using pdb and getting some import issues. The dependencies are installed in one of my bespoked virtualenv environments.
Pdb is stubbornly using /usr/bin/python and not the python process from my virtualenv.
I use virtualenv.el to support switching of environments within emacs and via the postactivate hooks described in
http://jesselegg.com/archives/2010/03/14/emacs-python-programmers-2-virtualenv-ipython-daemon-mode/
This works well when running M-x python-shell
>>> import sys
>>> print sys.path
This points to all of my virtualenv libraries indicating that the python-shell is that of my virtualenv.
This is contradicted however by M-! which python, which gives /usr/bin/python
Does anyone know how I can tell M-x pdb to adopt the python process from the currently active virtualenv?

Invoke pdb like this:
python -m pdb myscript.py
Instead of
pdb myscript.py

python-shell uses variable python-default-interpreter to determine which python interpreter to use. When the value of this variable is cpython, the variables python-python-command and python-python-command-args are consulted to determine the interpreter
and arguments to use. Those two variables are manipulated by virtualenv.el to set the current virtual environment.
So when you use python-shell command, it uses your virtual environments without any problem.
But, when you do M-! python, you're not using the variables python-python-command and python-python-command-args. So it uses the python tools it finds in your path.
When you call M-x pdb it uses gud-pdb-command-name as the default pdb tool. To redefine this variable, each time you activate an environment, you could do something like this :
(defadvice virtualenv-activate (after virtual-pdb)
(custom-set-variables
'(gud-pdb-command-name
(concat virtualenv-active "/bin/pdb" ))))
(ad-activate 'virtualenv-activate)
To have pdb in your virtual environment, do the following :
cp /usr/bin/pdb /path/to/virtual/env/bin
Then edit the first line of /path/to/virtual/env/bin/pdb to have :
#! /usr/bin/env python
Reactivate your env and Pdb should now use your virtualenv python instead of the system-wide python.

Possibly, your pdb command is tied to a certain specific version.
$ ls -ald /usr/bin/pdb
lrwxrwxrwx 1 root root 6 Jun 2 23:02 /usr/bin/pdb -> pdb2.6
Then, look at the first line of pdb2.6. It contains
#! /usr/bin/python2.6
This is why pdb is stubborn and always seems to run under a specific version of Python. Because it really is! Actually, this sort of dependency makes sense for a piece of software like a symbolic debugger.
I've compiled python2.7 from sources and pdb is not there, apparently.
After some scrutiny, I found pdb.py for python-2.7, under the lib folder.
I've then created some symlinks to it, for convenience:
$ cd /opt/python-dev ##-- this is where I installed from sources
$ cd bin
$ sudo ln -s ../lib/python2.7/pdb.py pdb2.7
$ sudo ln -s pdb2.7 pdb
Now observe the first line of pdb2.7. It reads:
#! /usr/bin/env python
... which looks better than the previous version. It basically means that pdb will be launched under the current Python you have defined in your environment, whatever it is, instead of anything hardcoded, like /usr/bin/python or /usr/bin/python2.6 are. Good to know!
I've also removed pdb and pdb2.6 from system files, once I prefer to develop/debug inside virtualenv. Doing that, I will not be caught again by the same trick.
I hope it helps.

A quick work-around is to explicitly call the python interpreter in you virtual env.
M-x pdb, then:
path/to/your/virtualenv/python3 -m pdb your_source.py

Related

path to /opt/python3.5.0/bin for the usage of python3

I installed python3.5.0 in /opt/python3.5.0/bin/, but I couldn't use it beyond this bin folder. I know this is a path issue. Could somebody point out the correct procedure to make python3 show up in the system for use? The operating system is CentOS6.6
The os needs to know where your python executable is.
Try running it from somewhere else
cd
/opt/python3.5.0/bin/python -V
If that works the your Python3 installation in opt is ok and you just need to let your shell (probably bash) know where the python binary is by changing your PATH environment variable.
export PATH=/opt/python3.5.0/bin/:$PATH
Run that line on the bash terminal and try running python again with no path like this:
python -V
If that works put the export PATH=/opt/python3.5.0/bin/:$PATH line in your .bashrc or .bash_profile so it will work in the console in future.
Failing all that take a look at setting the PYTHONHOME and PYTHONPATH environment variables.
Also a better approach might be to install python3 using your package manager. The python3 executable should give you a version of Python-3 if you have one installed. Though you might get a different version of Python3 than 3.5 that way.

Running script without virtualenv activation

Is there a difference between running script using virtualenv interpreter (without virtualenv activation) and running it in activated virtualenv?
venv/bin/python some_script.py
vs
source venv/bin/activate
python some_script.py
Running source bin/activate will set the PATH variable to point to your environment bin directory which is useful if you have other command line scripts/binaries installed (this can happen with certain python packages that add shell commands), it will also unset/set PYTHONHOME.
So, if bin/python works for you then you're fine but if some of the packages you're using start behaving strangely (or wrong one gets imported) it's probably because Python is getting the wrong PYTHONHOME or because a certain script is not found in PATH.
If you directly run a script or the python interpreter from the virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or /path/to/ENV/bin/python-script.py) then sys.path will automatically be set to use the Python libraries associated with the virtualenv. But, unlike the activation scripts, the environment variables PATH and VIRTUAL_ENV will not be modified. This means that if your Python script uses e.g. subprocess to run another Python script (e.g. via a #!/usr/bin/env python shebang line) the second script may not be executed with the same Python binary as the first nor have the same libraries available to it. To avoid this happening your first script will need to modify the environment variables in the same manner as the activation scripts, before the second script is executed.
source: https://virtualenv.pypa.io/en/16.7.9/userguide.html#activate-script
Yes. Virtualenv creates an interpreter in its own right. Just do this,
which python
For each interpreter, virtualenv and your normal interpreter and see what happens. They will show you two different links to python interpreter. Here's my example:
quazinafiulislam#Nafiuls-Mac: ~/Code/Python/PyTestingZone
$ which python [7:49:26]
/Users/quazinafiulislam/.pyenv/shims/python
quazinafiulislam#Nafiuls-Mac: ~/Code/Python/PyTestingZone
$ source .venv/bin/activate [7:49:29]
(.venv)
quazinafiulislam#Nafiuls-Mac: ~/Code/Python/PyTestingZone
$ which python [7:49:35]
/Users/quazinafiulislam/Code/Python/PyTestingZone/.venv/bin/python

Installing Python 3.3 on mac

I used the installer from http://www.python.org/download. The install appeared successful, and it dropped the Python 3.3 folder in my Applications directory. I ran the "Update Shell Profile.command" script it contained, and it prepended /Library/Frameworks/Python.framework/Versions/3.3/ to my path. Yet the Python version in that directory appears to be 2.7.5.
/Library/Frameworks/Python.framework/Versions/3.3 ls
Headers Python Resources bin include lib share
/Library/Frameworks/Python.framework/Versions/3.3 Python --version
Python 2.7.5
Also, ls /usr/bin | grep python shows:
python
python-config
python2.5
python2.5-config
python2.6
python2.6-config
python2.7
python2.7-config
pythonw
pythonw2.5
pythonw2.6
pythonw2.7
What have I missed?
There are multiple problems here.
First, you should not be running Python, the framework's executable. Framework executables aren't meant to be run, and it's really only a coincidence that this one happens to work.
Frameworks with programs meant to be run put them in a bin directory somewhere—either outside the framework (like /usr/local/bin) or inside it (like /Library/Frameworks/Foo.framework/Versions/X.Y/bin). By default, Python 3.3 does the latter.
And the programs inside the bin directory are all lowercased, not capitalized, so there is no Python to run.
Next, on Mac, and on almost every other platform in the world besides Windows, the current working directory is not part of your PATH. So, when you type Python --version, that finds Python somewhere on the PATH. The fact that you happened to have an executable of that name in the current directory doesn't mean anything (except that it's confusing to you). If you really want to run that file (but again, you really don't), you have to write ./Python instead.
Also, there is really no good reason to cd into the framework directory in the first place. Sure, you could run the file you want, from there, with the appropriate relative pathname: bin/python3, for example, but you don't want to.
Next, likely you're using a shell you already had running before installing Python 3.3. The Update Shell Profile.command script can be used to add Python 3.3 to the PATH for all future shells, or to spawn a new shell with that PATH, but either way it will not affect any existing shells. To solve that, you just have to start a new shell.
Next:
ls /usr/bin | grep python shows:
The /usr/bin directory is only for programs that are part of the OS. Programs you install yourself go in /usr/local/bin, or somewhere else on your PATH, instead. The Python installer has an option (although it may be hidden, I can't remember…) to put launchers in /usr/local/bin. And it also has an option—which you selected—to put its framework bin directory onto your PATH. But either way, it's never going to put anything in /usr/bin.
And finally, even after installing Python 3.3, the default python will still be 2.7. python3 and python3.3 will be 3.3, but python and python2.7 will be 2.7. See PEP 394 — The "python" Command on Unix-Like Systems for the rationale. But the short version is, there's all kinds of code that depends on Python 2.7 and isn't compatible with 3.3 that may be installed on your system, and you don't want it all to stop working.
So, putting it all together:
Create a new tab or window in Terminal.app.
Type python3 --version.
You may want to consider using a virtualenv:
$ /Library/Frameworks/Python.framework/Versions/3.3/bin/python3 -m venv ~/myvenv
$ source ~/myvenv/bin/activate
(myvenv) $ curl https://bootstrap.pypa.io/get-pip.py | python
(myvenv) $ deactivate
$ source ~/myvenv/bin/activate
http://docs.python-guide.org/en/latest/dev/virtualenvs/

How to run python in different directory?

I am doing maintenance for a python code. Python is installed in /usr/bin, the code installed in /aaa, a python 2.5 installed under /aaa/python2.5. Each time I run Python, it use /usr/bin one. How to make it run /aaa/python2.5?
Also when I run Python -v; import bbb; bbb.__file__; it will automatically show it use bbb module under /usr/ccc/(don't know why), instead of use bbb module under /aaa/python2.5/lib
How to let it run python2.5 and use `/aaa/python2.5/lib' module? The reason I asking this is if we maintain a code, but other people is still using it, we need to install the code under a new directory and modify it, run it and debug it.
Change the shebang-line (if there is such a line)
Nomally
#!/usr/bin/env python
is used to start the python interpreter first found in the path, this is how virtualenv works, i.e. it changes the path so that the chosen interpreter is placed first and the line above simply works.
In your example, change i to
#!/aaa/python2.5
Note that the executable-flag has to be set for this to work.
Example:
$ chmod u+x script.py
$ ./script.py
See PYTHONPATH for a description of how python searches for modules.
Do /aaa/python2.5 python_code.py. If you use Python 2.5 more often, consider changing the $PATH variable to make Python 2.5 the default.
Add this to your .bash_profile file (create if non exist, it's a hidden file):
python25(){
/aaa/python2.5 "$1"
}
Now you can use Python (latest) and Python 2.5:
$ python my_file.py
$ python25 my_file.py
Good luck!

how to make my epd python as my default python in ubuntu?

I recently installed epd python distribution in ubuntu. This got installed in the folder /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64
Can you tell me how to make this python as my default python?
I get errors while running a test program (it seems my default python is different and it doesn't have numpy library, other libraries that come along epd python distribution.)
My test program is here: http://www.southampton.ac.uk/~fangohr/computing/download/python/tests/testall.py
The "default" python depends on how you're invoking it.
On Ubuntu, python is normally installed as /usr/bin/python (not /bin/python) -- which may be a symbolic link.
If you invoke the python command, e.g.:
$ python myscript.py
it will use whichever python executable is in a directory that appears first in your $PATH. You can modify your $PATH, either for your current shell:
export PATH="/some/dir:$PATH"
or for all future shells by updating your $HOME/.bashrc, $HOME/.bash_profile, or whatever. /usr/local/bin is one common place to put system-specific executables, or $HOME/bin for user-specific executables.
If you want to execute the script itself, you'll need a shebang as the first line of the script:
$ head -1 myscript.py
#!/usr/bin/python
$ ./myscript.py
...
You can edit the shebang to refer to whatever Python executable you want to use.
You can replace /usr/bin/python with your preferred Python executable, but that might cause unwanted side effects; existing Python scripts that assume /usr/bin/python is the default might break.
Another option is to change the shebang to:
#!/usr/bin/env python
which lets you execute the script directly while still using whichever python is first in your $PATH. This may or may not be a good idea; see my answer to this question for further discussion.
Default python is the one found in /usr/bin directory with the name python. Making a symbolic link of :
ln -s /home/jai/Downloads/epd_free-7.3-2-rh5-x86_64 /usr/bin/python
Assuming that is the name of the python executable, not the installer. After you installed, use the path where you installed it. f.e /home/iai/myNewPythonInstallation
might do the trick.
Most likely default 2.7 python is occupying that name, so you need to remove that, or use another name like epdPython. Then running python scripts would happen with:
epdPython myscript.py

Categories