Testing a python script in a specific version

守給你的承諾、 提交于 2019-12-11 03:22:59

问题


I currently have Python 2.6.2 installed on my mac. I am writing a script which MUST run on Python 2.5.2. So I want to write a python script, and test is specifically against 2.5.2 and NOT 2.6.2.

I was looking at virtualenv, but it doesn't seem to solve my problem. I ran python virtualenv.py TEST which made a TEST dir, but it had python 2.6 in it. Is there a way to make virtualenv use a different version of python than what's installed default on my machine? Is another way to use the #! as the first line of the python script? How would I do that?


回答1:


You could just install a Python 2.5.2.

I have 3 different versions Python installed on my Lucid and they use different links under /bin/ so it's easy to call the specific version

python -> python3 ->python3.1
python2 -> python2.7
python2.5




回答2:


Check out tox; it's designed to do exactly this.




回答3:


You can setup a sandboxed environment with different python versions using virtualenv. As Kable has done, install the 2.5. version you want to test against. Then create your virtual environment:

virtualenv --p=python2.5 myapp

To get a clean environment you may use the --no-site-packages switch with the command above. Quite handy when trying to simulate a new, fresh setup. Now activate your virtualenv:

source myapp/bin/activate

If you check the python version you should now get version 2.5.x:

python -V

Now you can install modules as you like into your virtual environment in the usual fashion:

easy_install ... 
pip ...

To exit your virtual environment:

deactivate

Hope this may be of help.




回答4:


try #!/path/to/your/python/version

But make sure you execute the script from the terminal, and make it executable before hand: chmod 755 myscript.py




回答5:


Using 'virtualenv' you can have different isolated Python environments on a single machine. Also you can switch any-time between the different python interpreter versions.

What is virtualenv?

A Virtual Environment is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. It enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated.

How to install?

pip install virtualenv

To create virtual environment for python 2.7 :

root:~# which python2.7

/usr/bin/python2.7

root:~# which python3.4

/usr/local/bin/python3.4

You can also use a Python interpreter of your choice:

root:~# virtualenv -p /usr/bin/python2.7 Vpy27

Running virtualenv with interpreter /usr/bin/python2.7

New python executable in /root/Vpy27/bin/python2.7

Also creating executable in /root/Vpy27/bin/python

Installing setuptools, pip, wheel...done.

To begin using the virtual environment, it needs to be activated:

root:~# source Vpy27/bin/activate

The name of the current virtual environment will now appear on the left of the prompt:

(Vpy27) root:~# python -V
Python 2.7.3

Install packages as usual, for example:

(Vpy27) root:~# pip install junos-eznc    >> All pip installs done here, will be available only in this environment.

If you are done working in the virtual environment for the moment, you can deactivate it:

(Vpy27) root:~# deactivate   

To create virtual environment for python 3.4:

root:~# which python3.4

/usr/local/bin/python3.4

root:~# virtualenv -p /usr/local/bin/python3.4 Vpy34

root:~# source Vpy34/bin/activate

(Vpy34) root:~# python -V
Python 3.4.4

There is also a way to create virtual environment with already available site-packages.



来源:https://stackoverflow.com/questions/4301681/testing-a-python-script-in-a-specific-version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!