[CentralOH] How to Avoid Hard Coded Python Version?

Joe Shaw joe at joeshaw.org
Thu Nov 5 22:09:12 EST 2015


Hi,

You can get the Python version in a structured way from the
sys.version_info object.

There may be other ways to do what you want with sys.path or $PYTHONPATH as
well.

Joe

On Thu, Nov 5, 2015 at 9:37 PM, <jep200404 at columbus.rr.com> wrote:

> How would you avoid hard coding a Python version number?
>
> I've been porting a Django web server from Centos 6 to Centos 7.
> Apache was not happy on Centos 7.
> I figured out the problem was not an Apache configuration file.
> It was a hard coded python version number in a python file:
>
>     vepath = '{home}/env/lib/python2.6/site-packages'.format(home=apphome)
>
> So I changed it to the following, and Apache is happy on Centos 7.
>
>     vepath = '{home}/env/lib/python2.7/site-packages'.format(home=apphome)
>
> I would like to avoid that hard coded python version for the
> next time. I can imagine several ways of dynamically figuring
> out the python version. One would be to use a glob like
> '{home}/env/lib/python*/site-packages'.format(home=apphome)
> Another would be something like:
> '{home}/env/lib/python{version}/site-packages'.format(
>     home=apphome, version=sys.version[:3])
> The 3 in sys.version[:3] is ugly, so the glob feels slightly better.
>
> What would you use?
> What improvements do you have?
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20151105/2cce8d26/attachment.html>


More information about the CentralOH mailing list