Use SublimeREPL as Django shell - sublimetext

I am learning Django, and Django shell is a frequently used thing, but it is a bit frustrating to switch back and forth to the Terminal window.
I try to use SublimeREPL-shell, but it does not work properly. For instance, I can use python manage.py shell to enter the interactive console in the REPL window, but after that all command and results will not be displayed: it look something like this
bash: no job control in this shell
bash-3.2$ python manage.py shell
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
Well, the results will be displayed if I quit() and they will all show up (but a bit too late...). Something like this
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> >>> [<Person: John>, <Person: Jane>]
>>> bash-3.2$
So I wonder if this is a way to do it properly?

New a .py file in the path of manage.py. it contain these.
#!/usr/bin/env python
# run Django shell in SublimeREPL
# how to use: Meuns > Tools > SublimeREPL > Python > Python - RUN current file
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[your app].settings")
from django.core.management import execute_from_command_line
execute_from_command_line(['.', 'shell'])
How to use: Menus > Tools > SublimeREPL > Python > Python - RUN current file

Related

Import Bio module from Bash

I'm working on a remote server with Linux.
I created a new environment and got biopython installed with conda:
conda create --name new_bio python=3.5
conda activate new_bio
conda install -c conda-forge biopython
import Bio works when I am running it inside python:
(new_bio) -bash-4.2$ python
Python 3.5.5 | packaged by conda-forge | (default, Jul 23 2018, 23:45:43)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import Bio
>>> exit()
import Bio works when I am running my script like this:
(new_bio) -bash-4.2$ python my_Bio_script.py
hello
my_Bio_script.py:
from Bio import SeqIO
print('hello')
But when I try to run my script with Bash :
(new_bio) -bash-4.2$ sbash my_bash.sh
my_bash.sh bash file:
#!/bin/bash
module load anaconda ### load anaconda module
source activate new_bio ### activating environment, environment
must be configured before running the job
python my_Bio_script.py
I get this error:
Traceback (most recent call last):
File "T.py", line 1, in <module>
from Bio import SeqIO
ModuleNotFoundError: No module named 'Bio'
I have to send it as a Slurm Job in a bash file.. what can I do?
One potential way to start debugging is to print environment variables PYTHONPATH and PATH.
What I would recommend is, print $PATH and $PYTHONPATH. Analyse the python version and path, replicate the same inside the bash script.
Also, analyse the ~/.profile as well as ~/.bashrc file and see whether there is any discrepancy with PATHs for python.

Why is python's pip install behaving strangely in a script but fine in python prompt?

I'm trying to install a module from withing a python script using pip. Here is the contents of script.py:
#/usr/bin/python2.7
# I'm the file called `script.py`
import sys, importlib, pip
print(sys.version); print(sys.path) # For debugging
try:
importlib.import_module('docopt')
except ImportError:
pip.main(['install', '-U', 'docopt'])
finally:
globals()[pack] = importlib.import_module('docopt')
Runnting this script, e.g. using python2.7 script.py gives me:
$ python2.7 script.py
2.7.9 (default, Oct 3 2016, 17:42:24)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)]
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages']
No module named docopt
Downloading/unpacking docopt
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement docopt
Cleaning up...
No distributions at all found for docopt
Storing debug log for failure in /root/.pip/pip.log
Traceback (most recent call last):
[...]
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named docopt
So problems with internet connection/ssl or something transport related. However, the same commands work perfectly fine in the python interactive interpreter, when I enter them by hand (copy&paste them):
$ python2.7
Python 2.7.9 (default, Oct 3 2016, 17:42:24)
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip, sys
>>> print(sys.path)
['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages']
>>> pip.main(['install', '-U', 'docopt'])
Downloading/unpacking docopt
Downloading docopt-0.6.2.tar.gz
Running setup.py (path:/tmp/pip_build_root/docopt/setup.py) egg_info for package docopt
Installing collected packages: docopt
Running setup.py install for docopt
Successfully installed docopt
Cleaning up...
0
Again, the thing that weirds me out is, that it works interactively i.e. running python2.7 in the shell and then entering the very same code by hand. However, not when I run the script file with python2.7 script.py. So In one case, on the same machine, the same interpreter has internet access in the other case it does not.
I'm running out of troubleshooting ideas. I used the same machine and user to get above results. There is no python-startup file, so that is not making the commands magically work interactively. The contents of sys.path are the same in both cases. I'm not behind a proxy. Any ideas what could be missing in the script?
Cannot fetch index base URL https://pypi.python.org/simple/
indicates that for some reason your python interpreter cannot access to internet. There can be plenty of reasons for that. Do you use a HTTP proxy ?

ipython can't load a module which python does

I have recently updated to OSX El Capitain upgrading an exhisting working osx installation. Now I have a module, installed via pip on an anaconda distribution which is now broken. Or better the module can be correctly imported from python
Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Sep 15 2015, 14:29:08)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import MDSplus as mds
whereas it does not load on ipython, claiming not to finding a library.
Python 2.7.10 |Anaconda 2.3.0 (x86_64)| (default, Sep 15 2015, 14:29:08)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: MacOSX
In [1]: import MDSplus as mds
Error importing MDSplus package: Error finding library: MdsShr
The library is correctly installed, it is listed in /etc/profile, it is located in /usr/local (so there should not be a problem with the new security system of OSX el Captain) and the python interpreted which is called via python or ipython is the same (Anaconda 2.3.0).
How can I diagnose which is the problem?
EDIT:
the solution proposed in https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/PEuOKEM5fdc does not worked for me. It does not seem to be an environment problem and the command
echo $DYLD_LIBRARY_PATH
points to the correct path
EDIT:
which -a ipython
ipython is /Users/vianello/anaconda/bin/ipython
which -a python
python is /Users/vianello/anaconda/bin/python
python is /usr/bin/python
>>> import sys
>>> for x in sys.path: print x
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/pyhht-0.0.1-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/MitDevices-0.3-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.157-py2.7.egg
>>>//anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.147-py2.7.egg/MDSplus
>>>/Users/vianello/anaconda/lib/python27.zip
>>>/Users/vianello/anaconda/lib/python2.7
>>>/Users/vianello/anaconda/lib/python2.7/plat-darwin
>>>/Users/vianello/anaconda/lib/python2.7/plat-mac
>>>/Users/vianello/anaconda/lib/python2.7/plat-mac/lib-scriptpackages
>>>/Users/vianello/anaconda/lib/python2.7/lib-tk
>>>/Users/vianello/anaconda/lib/python2.7/lib-old
>>>/Users/vianello/anaconda/lib/python2.7/lib-dynload
>>>/Users/vianello/anaconda/lib/python2.7/site-packages
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/PIL
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/aeosa
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/lmfit-0.8.3-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/setuptools-18.3.2-py2.7.egg
>>>/Users/vianello/anaconda/lib/python2.7/site-packages/suds-0.4-py2.7.egg
in iPython the same give the results
/Users/vianello/anaconda/bin
/Users/vianello/anaconda/lib/python2.7/site-packages/pyhht-0.0.1-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/MitDevices-0.3-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/MDSplus-alpha_7.0.157-py2.7.egg
/Users/vianello/anaconda/lib/python27.zip
/Users/vianello/anaconda/lib/python2.7
/Users/vianello/anaconda/lib/python2.7/plat-darwin
/Users/vianello/anaconda/lib/python2.7/plat-mac
/Users/vianello/anaconda/lib/python2.7/plat-mac/lib-scriptpackages
/Users/vianello/anaconda/lib/python2.7/lib-tk
/Users/vianello/anaconda/lib/python2.7/lib-old
/Users/vianello/anaconda/lib/python2.7/lib-dynload
/Users/vianello/anaconda/lib/python2.7/site-packages/Sphinx-1.3.1-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/lmfit-0.8.3-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/setuptools-18.3.2-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages/suds-0.4-py2.7.egg
/Users/vianello/anaconda/lib/python2.7/site-packages
/Users/vianello/anaconda/lib/python2.7/site-packages/PIL
/Users/vianello/anaconda/lib/python2.7/site-packages/aeosa
/Users/vianello/anaconda/lib/python2.7/site-packages/IPython/extensions
/Users/vianello/.ipython
Thus the MDSplus-alpha_7.0.157-py2.7.egg can be seen both from python and ipython
My first thought is that you are running afoul of the new Apple System Integrity Protection. iPython is launched using a shell script that loads via /bin/bash. That will result in your DYLD_LIBRARY_PATH environment variable being stripped when python launches. You can check this by importing os and then attempting to print the contents of os.environ["DYLD_LIBRARY_PATH"]. If that print throws an exception then you know what the problem is.
A simple fix that works for me is to edit the first line of the ipython script so that the #! calls your python binary directly rather than going via bash. This is not a long term solution as it will have to be redone each time ipython is updated until upstream changes (to be fair I'm not sure why bash is involved).
I have written a report on python and library paths at http://dmtn-001.lsst.io

Invoking an earlier version of python in cmd

I've installed Python 2.7 and python 3.4 on my system. Earlier i was using 3.4 but now require 2.7 as well (for a course I'm taking, which requires some files which are not there for 3.4). When I type python in cmd it by default initializes 3.4. Is there some way to change it to 2.7 by default?
Thanks
UPDATE: I was able to invoke it using 'py'. Here's the link if anyone else has a confusion. https://docs.python.org/3/using/windows.html?highlight=shebang#python-launcher-for-windows (from some other answer)
Update:
If your system environment is Windows:
1) call py -2.7 or py -3 based on the version you want.
2) change your PATH system environment variable , makes sure it refers to the version you want.
You can do it in serveral ways on Linux environment:
1)
call a specific Python binary you can call the explicit full path (e. g. /usr/local/bin/python). This will call the same executable regardless of the value of the $PATH variable.
2)
configure your accounts so that both have the same binary first in the $PATH variable.
3)
call python2 and python3; many installations symlink these to the corresponding binaries.
$ python2
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Listing serial ports in Mac OS X and Python 3

I'm having trouble listing available serial ports and I really need help debugging this.
In Python 2.7.5 the COM-ports are listed correctly while PySerial returns an empty list in Python 3.3.5.
I found one other lonely soul with the same problems on the internet (no answers), but the problem doesn't seem to be popular at all - maybe it's my system?
I'm using Mac OS X 10.9.2 and installed python and python3 via homebrew. I updated everything just now. PySerial is at version 2.7 in both pip and pip3.
The output:
Python 2.7.5 (default, Nov 4 2013, 18:04:45)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from serial.tools import list_ports
>>> list_ports.comports()
[['/dev/cu.Bluetooth-Incoming-Port', 'n/a', 'n/a'], ['/dev/cu.Bluetooth-Modem', 'n/a', 'n/a']]
Python 3.3.5 (default, Mar 10 2014, 13:25:50)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from serial.tools import list_ports
>>> list_ports.comports()
[]
For the moment I'm resorting to the following method. The PySerial tools are broken.
>>> import glob
>>> glob.glob('/dev/tty.*')
['/dev/tty.Bluetooth-Incoming-Port', '/dev/tty.Bluetooth-Modem']
The root cause of the problem stems from the fact that python3 encodes strings as unicode (wide) strings, and python2 encodes strings as narrow strings.
So the pyserial code needs to pass down narrow strings when calling the API functions from iokit.
I discovered that somebody else also ran into this and posted a patch. You can find his patch at the end of this issue:
http://sourceforge.net/p/pyserial/patches/38/
Using that patch, I now get the same behaviour from python3 as I do from python2.
I had installed python3 using macports, and my installation of pyserial for python3 was found in this directory:
/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/serial
so I executed the following:
cd /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/serial/tools
sudo cp list_ports_osx.py list_ports_osx_orig.py
sudo curl -O http://sourceforge.net/p/pyserial/patches/_discuss/thread/603bd426/55a8/attachment/list_ports_osx.py
and that made list_ports.comports() work for me.

Resources