开发者

Testing a python script in a specific version

开发者 https://www.devze.com 2023-01-27 14:41 出处:网络
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 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 tha开发者_开发技巧t?


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

Here is an example of using tox to run a hello_world.py script against multiple Python versions:

Install tox

pip install tox

Create a tox.ini configuration file

[tox]
envlist = py39, p310, p311

[testenv]
commands = python hello_world.py
  • The envlist option specifies the Python versions to test against
  • The commands option specifies the command to run in each environment

Run tox

tox
  • This will create separate environments for Python 3.9, 3.10, and 3.11 and run the hello_world.py script in each environment.


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消