Alexis Eggermont
Alexis Eggermont

Reputation: 8165

update python module while it is active

Is it possible to update a python module while it is used in a running script?

The situation is the following: 1) I have a script running using pandas 0.15.2. It is a long data processing task and should continue running for at least another week. 2) I would like to run, on the same machine, another script, which requires pandas 0.16.

Is it possible for me to do 2) without interrupting 1)?

Upvotes: 1

Views: 58

Answers (2)

Wheat
Wheat

Reputation: 885

Install pandas 0.16 in an alternate location. For example on my system I made the directory /Users/kteague/pytest/ for installation. Then I used the --target option in pip to install into that location:

$ pip install --target /Users/kteague/pytest pandas
Collecting pandas
  Using cached pandas-0.17.0-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting pytz>=2011k (from pandas)
  Using cached pytz-2015.6-py2.py3-none-any.whl
Collecting python-dateutil>=2 (from pandas)
  Using cached python_dateutil-2.4.2-py2.py3-none-any.whl
Collecting numpy>=1.7.0 (from pandas)
  Using cached numpy-1.10.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting six>=1.5 (from python-dateutil>=2->pandas)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: pytz, six, python-dateutil, numpy, pandas
Successfully installed numpy pandas python-dateutil pytz six-1.9.0

Now you can export your PYTHONPATH to point to that location first. Scripts run from a terminal where the PYTHONPATH=/Users/kteague/pytest will use pandas 0.1.7 over whatever version of pandas is installed in the default site-packages directory.

$ export PYTHONPATH=/Users/kteague/pytest/

Use setuptools from the python interpreter to ensure your terminal is importing the version of pandas that you want:

$ python
Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources
>>> pkg_resources.require("pandas")[0].version
'0.17.0'

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 600059

If the script is still running, it's likely that replacing the dependency will not affect it at all - the code will already be in memory.

Still, it's better to be safe than sorry. I would install the other script inside a virtualenv, in which you can install whichever versions of modules you want without affecting anything else.

Upvotes: 3

Related Questions