How to get Automator to recognize python script with pyperclip? - python

I'm using Automator in Mac OSX 10.12.3 to run a Python script utilizing pyperclip. I created an Automator step with Run Shell Script (/usr/bin/python):
import pyperclip
import datetime
now = datetime.datetime.now()
pyperclip.copy(str(now.year) + str(now.month) + str(now.day) + '_' + str(now.hour) + str(now.minute) + str(now.second))
However, it is showing this error:
ImportError: No module named pyperclip
I can execute this code in Terminal, why pyperclip is not recognized by /usr/bin/python ? FYI, I also have Anaconda installed.

Automator is using /usr/bin/python while I installed pyperclip under Anaconda virtualenv. When I run python, I noticed it said :
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Look like it is a different python version compared to the one Automator is using. Now all I have to do is:
sudo /usr/bin/python -m pip install pyperclip

Related

Move from python 2 to python 3 on Mac

I have both python 2.7.16 and python 3.7.3 on my Macbook air.
I don't use python 2.7.16 so I want to remove it, but I understood that this could break my Mac.
I am frustrated from using python3 and pip3 instead of python and pip is there a way to make all of the python3 commands to be accessed by using python (without 3) instead of python 2 and make python 2 be accessible by using python2?
tnx ahead
(base) shrub$ /usr/bin/py
pydoc python python2.7 pythonw
pydoc2.7 python-config python2.7-config pythonw2.7
Looking in /usr/bin there is a python executable (which when run opens a shell with python3) and a python2.7 executable (which when run opens a shell with python2).
(base) shrub$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
(base) shrub$ python2.7
Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Running python scripts with python (python hello.py for example) should default to python3 and running scripts with python2.7 (python2.7 hello.py for example) will run them with python2.
Also there is a script called 2to3 which can help you with converting your python2 code to python3 :)
https://docs.python.org/3.0/library/2to3.html

Did installing Miniconda destroy Python 2.7 on my Mac?

Have I lost Python 2.7 and does Mac OS still need it? If so, how do I fix this?
I'm a longtime Python 3.x user on Mac OS. I wanted to try Miniconda3 to see if I could recommend it to Mac and Windows students (non-CS). Now I when I try $ type -a python3 I get:
python3 is /Applications/miniconda3/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
python3 is /usr/local/bin/python3
Try $ type -a python2 and get:
-bash: type: python2: not found
Try $ type -a python and get:
python is /Applications/miniconda3/bin/python
python is /usr/bin/python
I used the Miniconda 64-bit (.pkg installer) under Python 3.7 here:
https://docs.conda.io/en/latest/miniconda.html
Type /usr/bin/python and:
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Okay, it's all good. Thanks, juanpa.arrivillaga!

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

How do I add tab completion to the Python shell?

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

Categories