Force Python to Use an Older Version of Module (Than What I Have Installed Now)

Force python to use an older version of module (than what I have installed now)

A better version of option B. would be to replace

import twisted

by

import pkg_resources
pkg_resources.require("Twisted==8.2.0")
import twisted

which will arrange for the correct version of twisted to be imported, so long as it's installed, and raises an exception otherwise. This is a more portable solution.

This won't work, though (nor would any other variaton of option B), if twisted gets imported before the pkg_resources.require gets called; twisted will already be in sys.modules

OP Edit: Minor syntax correction, per pkg_resources docs

How do I 'force' python to use a specific version of a module?

This is a very messy solution and probably shouldn't be encouraged but I found that if I remove the location of the old version of numpy from the system path I can call the version I want. The specific lines were:-

import sys
sys.path.append('C:/Python27/Lib/site-packages')
sys.path.remove('V:\\\Brian.140\\\Python.2.7.3\\\Lib\\\site-packages')
import numpy

Install a module using pip for specific python version

Use a version of pip installed against the Python instance you want to install new packages to.

In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).

pip's website includes installation instructions, if you can't find anything within your distribution.

Installing specific package version with pip

TL;DR:

  • pip install -Iv (i.e. pip install -Iv MySQL_python==1.2.2)

What these options mean:

  • -I stands for --ignore-installed which will ignore the installed packages, overwriting them.
  • -v is for verbose. You can combine for even more verbosity (i.e. -vv) up to 3 times (e.g. -Ivvv).

For more information, see pip install --help

First, I see two issues with what you're trying to do. Since you already have an installed version, you should either uninstall the current existing driver or use pip install -I MySQL_python==1.2.2

However, you'll soon find out that this doesn't work. If you look at pip's installation log, or if you do a pip install -Iv MySQL_python==1.2.2 you'll find that the PyPI URL link does not work for MySQL_python v1.2.2. You can verify this here: http://pypi.python.org/pypi/MySQL-python/1.2.2

The download link 404s and the fallback URL links are re-directing infinitely due to sourceforge.net's recent upgrade and PyPI's stale URL.

So to properly install the driver, you can follow these steps:

pip uninstall MySQL_python
pip install -Iv http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.2/MySQL-python-1.2.2.tar.gz/download

How to move all modules to new version of Python (from 3.6 to 3.7)

Even if the old python version has been removed, it is possible to use the pip of the current python version with the --path option to list all the modules installed in the previous version.

For example, migrating all my user installed python modules from 3.7 to 3.8

pip freeze --path ~/.local/lib/python3.7/site-packages > requirements.txt
pip install --user -r requirements.txt

Incidentally, I always use pip install with --user and leave the system wide installations to the package manager of my linux distro.

What is the best way to use different versions of a function from a library in different scripts?

That is a pretty interesting question. Let's address them one by one:

1. Library versioning:

If you are publishing your library to PyPi or any other repository, you can have different versions, and install a specific version using pip. However, you cannot have multiple versions of the same library

2. Calling it different things:

This would the prefered method. However, I don't recommend naming it _1, _2. You should name it in a descriptive way so you understand the difference between the different functions. So based on your description, you'd have parse_csv and parse_csv_date_as_datetime.

3. Redirecting with arguments

In general this is not a good idea. Arguments should not fundamentally change how your function behaves. That can lead to unexpected behaviours. Parameters should be the data you perform computations on.

In conclusion

I recommend just creating a second function. Use the original in your past code, use the new one in the new code.

If you really must have a single function, you could try applying the Strategy Pattern to handle different data formatting tasks, passing in a strategy function and having a default what matches the current behaviour, this however I'd say is probably beyond your current level.

How to specify which pytorch to import, if I have two different versions installed?

if you have more than one version of a package installed you can use package resources as follows

import pkg_resources
pkg_resources.require("torch==1.7.1") # The version you want to import
import torch

reference



Related Topics



Leave a reply



Submit