How do I add tab completion to the Python shell? - python

When starting a django application using python manage.py shell, I get an InteractiveConsole shell - I can use tab completion, etc.
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
When just starting a python interpreter using python, it doesn't offer tab completion.
Can someone tell me what django is doing to give me an interactive console, or what I need to do to start an interactive console without a django app?

I may have found a way to do it.
Create a file .pythonrc
# ~/.pythonrc
# enable syntax completion
try:
import readline
except ImportError:
print("Module readline not available.")
else:
import rlcompleter
readline.parse_and_bind("tab: complete")
then in your .bashrc file, add
export PYTHONSTARTUP=~/.pythonrc
That seems to work.

I think django does something like https://docs.python.org/library/rlcompleter.html
If you want to have a really good interactive interpreter have a look at
IPython.

For the record, this is covered in the tutorial: http://docs.python.org/tutorial/interactive.html

I use ptpython - it is a wonderful tool autocomplete shell cmd.
Installing ptpython is very easy, use pip tool
pip install ptpython
and for django shell, you should import the django env, like this
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testweb.settings")
Trust me, this is the best way for you!!!

Fix for Windows 10 shell:
pip install pyreadline3 # previously, pyreadline but that package was abandoned
pip install ipython

It looks like python3 has it out-of box!

In Python3 this feature is enabled by default. My system didn't have the module readline installed. I am on Manjaro. I didn't face this tab completion issue on other linux distributions (elementary, ubuntu, mint).
After pip installing the module, while importing, it was throwing the following error-
ImportError: libncursesw.so.5: cannot open shared object file: No such file or directory
To solve this, I ran-
cd /usr/lib
ln -s libncursesw.so libncursesw.so.5
This resolved the import error. And, it also brought the tab completion in the python repl without any creation/changes of .pythonrc and .bashrc.

Yes. It's built in to 3.6.
fernanr#gnuruwi ~ $ python3.6
Python 3.6.3 (default, Apr 10 2019, 14:37:36)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 318 possibilities? (y or n)
os.CLD_CONTINUED os.O_RDONLY os.ST_NOEXEC os.environ os.getpid( os.readlink( os.spawnvpe(
os.CLD_DUMPED os.O_RDWR os.ST_NOSUID os.environb os.getppid( os.readv( os.st

For older versions (2.x) above script works like charm :)
fernanr#crsatx4 ~ $ cat .bashrc | grep -i python
#Tab completion for python shell
export PYTHONSTARTUP=~/.pythonrc
fernanr#crsatx4 ~ $ . ~/.bashrc
fernanr#crsatx4 ~ $ echo $?
0
fernanr#crsatx4 ~ $ python2
Python 2.7.5 (default, Jun 11 2019, 14:33:56)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.
Display all 249 possibilities? (y or n)
os.EX_CANTCREAT os.O_WRONLY

Related

different result in find_executable(): python vs sudo python

I'm writing this to ask what is the difference between executing python as normal user and previleged (i.e, sudo) user.
I have a python script that installs python package in specific directory (here, /usr/local is used), and in order to do that the script should be run with sudo.
The script seems calling external binary, however in sudo mode it fails to find it using find_executable(~), whereas it succeeds flawlessly without sudo command.
Here's code: calling script with & without sudo, respectively. Both codes have (nearly but impactless) identical contents.
Note that both python is identical, as I called it explicitly in sudo mode (I found that without specifying python binary path executes system-wide python).
w/ sudo:
sudo /home/.../anaconda3/envs/pytorch_open3d/bin/python
Python 3.8.8 (default, Feb 24 2021, 21:46:12)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from distutils.spawn import find_executable
>>> pyside2Uic = ["pyside2-uic", "python2-pyside2-uic", "pyside2-uic-2.7"]
>>> found_pyside2Uic = any([find_executable(p) for p in pyside2Uic])
>>> print(found_pyside2Uic)
False
w/o sudo:
which python
/home/.../anaconda3/envs/pytorch_open3d/bin/python
python
Python 3.8.8 (default, Feb 24 2021, 21:46:12)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from distutils.spawn import find_executable
>>> pyside2Uic = ["pyside2-uic"]
>>> found_pyside2Uic = any([find_executable(p) for p in pyside2Uic])
>>> print(found_pyside2Uic)
True
I also tried answer already provided (link), which is an argument that preserves current environment information, but has no effect:
sudo -E /home/.../anaconda3/envs/pytorch_open3d/bin/python
Python 3.8.8 (default, Feb 24 2021, 21:46:12)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from distutils.spawn import find_executable
>>> pyside2Uic = ["pyside2-uic"]
>>> found_pyside2Uic = any([find_executable(p) for p in pyside2Uic])
>>> print(found_pyside2Uic)
False
Is there anything I missed? Any helps are greatly appreciated. Thanks in advance.
ps. result of echo
echo $PATH
/home/.../anaconda3/envs/pytorch_open3d/bin:/home/.../anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
sudo echo $PATH
[sudo] password for ...:
/home/.../anaconda3/envs/pytorch_open3d/bin:/home/.../anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Thanks to #Nephanth for valuable discussion,
I've found that there's a mismatch between result of which pyside2-uic and sudo which pyside2-uic, latter gives no path to the binary.
So I searched to related problems, and found link. From the answer,
sudo env "PATH=$PATH" python
preserved path of normal user.

How to make python to choose a specific version of opencv?

I am using ubuntu14.04 and python2.7 and I have installed opencv3.2.0 (/usr/local) and opencv2.4.8(/usr/local/opencv/2.4.8) in my machine. The outcome of command
pkg-config --modversion opencv
is 2.4.8
while python script
print cv2.__version__
is 3.2.0.
What should I do to change it to 2.4.8?
=========================================================================
I have tried export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
It seems no use
$ export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
3.2.0-dev
>>>
Although It shows the version is 3.2.0, it is actually using the 2.4.8, thanks for IronFarm's answer
Add the directory for the v2.4.8 to the beginning of your PYTHONPATH environment variable before running Python.
On Linux:
export PYTHONPATH=/usr/local/opencv/2.4.8/:$PYTHONPATH

pip prints trash messages in stdout

When I try to use pip commands, annoying messages are coming out in stdout:
~# pip -V
Platform: linu
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)
~# pip install
Platform: linu
You must give at least one requirement to install (see "pip help install")
Python commands are working normally.
OS - Ubuntu 14.04
I tried to reinstall pip and all dependencies, but it didn't help.
What is that and where it comes from?
The problem was noticed when I tried to use ec2.py dynamic inventory script for AWS. I faced the same problem as here:
https://github.com/ansible/ansible/issues/14667
ec2.py generates JSON with starting "Platform: linu" and therefore ansible doesn't work with that.
Also I searched for boto library (used in ec2.py) and pip configs. But they are blank.
Any suggestions?
Python
~# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.name
posix
>>> import platform
>>> platform.system()
'Linux'
Found one more way to reproduce the issue:
:/usr/bin# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import os
>>> import boto
Platform: linu
>>>
This is common mistake. Most of this weird problem occur (whether in a plain PC, VM or inside cloud instance such as EC2. Did you notice AWS EC2 using a different locked down pip version) is running Python in sudo mode.
DO NOT run PIP in sudo mode!
My suggestion : setup Virtualenv and install your package on that virtualenv. Then use mkvirtualenv yourenv to create an custom virtualenv.
To load a python script automatically with the designated virtualenv, you just need to put an extra line inside your bash script to trigger your python package/modules.
source <virtualenv_folder>/<virtualenv_name>/bin/activate

python-Can't import module in Pycharm while I can import the same module in terminal?

I installed essentia by
brew install essentia --HEAD
message:
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/yangyy/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/yangyy/Library/Python/2.7/lib/python/site-packages/homebrew.pth
I did as the message instructed and installed essentia successfully.
The module can be imported in terminal:
Python 2.7.11 (default, Feb 23 2016, 00:59:46)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
-import essentia
-exit()
$ which python
/usr/local/bin/python
I use Pycharm as my IDE. I tried all the interpreter, but still can't import this module.
In python, I tried:
>>> import essentia
>>> reload(essentia)
<module 'essentia' from '/usr/local/lib/python2.7/site-packages/essentia/__init__.pyc'>
Pycharm wont take package dependencies instead it contains package you should initiate for your projects..
so, for that, You should go to
settings -> project Interpreter
then choose your project and check your packages are there then click,
apply <-button
that's it

Anaconda python not available from sudo

I installed Anaconda in a Google Cloud Compute environment and can use it successfully from the shell as a normal user:
curt#lamp-v5mi:~$ python
Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Mar 9 2015, 16:20:48)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
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://binstar.org
However, when I start an interpreter via sudo python, anaconda is not the interpreter used, and I would like it to be.
curt#lamp-v5mi:~$ sudo python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Confusingly, when I start a shell as root and then start an interpreter, anaconda is the interpreter used.
curt#lamp-v5mi:~$ sudo -s
root#lamp-v5mi:/home/curt# python
Python 2.7.9 |Anaconda 2.2.0 (64-bit)| (default, Mar 9 2015, 16:20:48)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
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://binstar.org
I have export PATH="/anaconda/bin:$PATH" in both the root's and my normal account's .bashrc files. At first I thought the issue was sudo python not actually starting a root shell, and thus the export PATH="/anaconda/bin:$PATH" not actually being done. But when from my normal account I do sudo echo $PATH, it shows anaconda in there:
curt#lamp-v5mi:~$ sudo echo $PATH
/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
The anaconda installation was installed as root in /anaconda and I did a chmod -R 770 /anaconda to make it accessible to normal users, but I don't think this problem has anything to do with that.
How can I get anaconda to be the default interpreter when run from a sudo command line?
You almost got everything right. The only error is sudo echo $PATH, where $PATH is substitued BEFORE being sent to sudo, so it's your user PATH not your "sudoed" PATH that's displayed.
Note that your sudo implementation and configuration may change the PATH variable, as I can read in a "man sudo" (found from the Internet as I don't have sudo):
PATH
May be overridden by the security policy.
So, in your "sudoed" PATH, there's probably no /anaconda/bin/
You may test this using sudo env | grep PATH.
To allow or change the PATH environment variable in your sudoed environment, I only can direct you to your man sudo, again: I'm not a sudo user.
The only thing I can drop you is sudo $(which python), as in sudo echo $PATH, the $(which python) will be executed by your user, resulting in /anaconda/bin/python, so actually running sudo /anaconda/bin/python which is another "solution".
To conclude, I should warn you that you probably don't want to run Python as root, there is almost no valid reason to do this, so your question is probably an XY problem: You got a first problem, you concluded by yourself it can be resolved by running Python as root, you tried sudo, got hit by sudo changing your PATH, then posted your "2nd level" problem here. What is your "real problem" ? The one that triggered the "Hum, I should try with sudo" ?

Categories