Tuesday, September 25, 2018

Python and Packages

Recently I started to watch a long training video on YouTube, 9+ hours. I just spent fraction of a few minutes daily as alternative refreshment. During the training period, I tired to use Idle app to practice some codes. Up to the point of installing third party packages, I could not get import to work on my Mac. I spent couple of days struggling and finally I figured it out. I think it is worthwhile to take some notes about this experience.

Tool to Install Packages


The first hurdle is that there is no pip, a tool to install third party python packages, available in terminal on Mac. It seems that python is part of Mac OS system, but the version of python is an old version: 2.5 & 2.7. pip was not installed on my Mac.

Eventually I got pip installed by using easy_install tool:

sudo easy_install install pip

After that, I could install pyperclip, a package for using clipboard copy and paste features.

sudo pip install pyperclip

However, the import of pyperclip in python shell and script still does not working!

Version Issue

I could not find out any solution from web easily. It seems that all the solutions in the top search list I found from web are old ones, ie for python 2.*. From one place I read a hint about the issue: different versions of python on Mac. It is true that python came with Mac OS is version 2.3, 2.5 & 2.7. The newest version, for the time being, I installed on Mac is python 3.7.

Further investigation, I found that the tool of pip is actually used to install packages for python 2.7. The pip is located at the path of:

/Library//Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip

The tool I need to install pyperclip is actually pip3! This one is at the path of:

/Library//Frameworks/Python.framework/Versions/3.7/bin/pip3

When I sorted it out, it is so easy to install third party packages for python 3.7. For example:

sudo pip3 install pyperclip

For my interest, I further found out the locations of packages for different versions. For python 2.7, the location of package is at:

  1. find /Library/Python/ -name pyperclip
  2. /Library/Python//2.7/site-packages/pyperclip

and for python version 3.7 it is at:

  1. find /Library/Frameworks/Python.framework/ -name pyperclip
  2. /Library/Frameworks/Python.framework//Versions/3.7/lib/python3.7/site-packages/pyperclip


Python also in Different Versions!


One more interesting thing I found out is that, on my Mac, the name of python executable also has different ones! For the python came with Mac OS is actually version 2.5, and the executable name is python:

/Library//Frameworks/Python.framework/Versions/2.5/bin/python

and for the newest version, 3.7 I installed, the name is python3:

/Library/Frameworks/Python.framework//Versions/3.7/bin/python3


From the terminal, the command of python will bring up to version 2.5:

  1. ...$ python
  2. Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
  3. [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
  4. Type "help", "copyright", "credits" or "license" for more information
  5. >>>

or type option -h to get all available options. -V is for getting python version:

  1. python -h
  2. ...
  3. Python 2.5


In order to run python3 from terminal, the PATH has to be changed: adding python3's path. This is the result of my updated PATH:

  1. echo $PATH
  2. /Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:

The PATH is set in the file of .bash_profile:

    more ~/.bash_profile

  1. # Setting PATH for Python 3.7
  2. # The original version is saved in .bash_profile.pysave
  3. PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}:/Users/dchu/Programming/py"
  4. export PATH

Even with this change, you have to type the correct python executable name, ie python3, to enter python shell:

  1. python3
  2. Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
  3. [Clang 6.0 (clang-600.0.57)] on darwin
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import pyperclip
  6. >>>

Finally, I could do import pyperclip in python shell and scripts without any errors.

By the way, here are the options to get all the commands, options, and the version for the tool of pip or pip3:

  1. pip -h
  2. ...
  3. pip -V
  4. pip 18.0 from /Library/Python/2.7/site-packages/pip-18.0-py2.7.egg/pip (python 2.7)
  5. pip3 -V
  6. pip 10.0.1 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)


References



Read More...

Monday, September 17, 2018

Access to iOS Device Sandbox Data

If you have some files, database for example, in your iOS app, you may need to access to them. Recently, while I was working my app update, I was not sure if a major change would cause to lose my database. Before I run the app on my iPhone, I would like to keep a copy of my database to avoid any risk to lose my data over years.

There are couple ways to do that.

Backup to iTunes


This is very common way to keep a backup of a whole iOS device. First lunch iTunes on a Mac/Windows. Then connect your iPhone to the Mac/Windows. Click on iPhone icon to open a view, where backup is available.

This is very convenient to keep a backup before any adventure or after a period of time. In case of screwup, you can easily to restore your iPhone back to your previous status.

However, with this method, you could not see any files in your app sandbox, nor you could make any changes.

Devices in Xcode


The second way to make a backup of an app is available in Xcode. First, set your iPhone as target or active scheme. From the menu of Windows->Devices and Simulators, open a view.



Here is the view:



Select the app you want to make a backup, then click the gear icon to bring up a menu list. In this way, the whole app sandbox is backed up to your local Mac. The content of the backup can be viewed through Show Package Contents context menu.



This is pretty cool! Not only I can easily to restore the sandbox back to my iPhone, I could also view the content what I have in the app on my iPhone device. For example, the core data database, as you can see, is actually based on several files, all beginning with the same prefix name, defaultDatabase, as I choose it as database name for my app.

When I run my app in Xcode simulator, not only I could view, but also I could access to my app sandbox from Finder. There is no need to download or upload files to my app sandbox in Simulator.

I think that I could make some changes in the backup and restore back with changes I need. This is really handy for iOS developers in case needed.

Notice that you can only access to the app you are in development and installed to the device from Xcode, and the app has to be on your device. You can not see any other apps. If your app is installed through App Store, or Test Flight, you could not see it. This is for security reason. Anyway, it is good enough for me.

The Xcode I use is version 9.4 and iOS is version 11.4.

References



Read More...