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?
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.
Related
I always used Anaconda on Windows so far and could set up an environment while choosing which exact Python to use. E.g. conda create -n myEnvName python=3.7
Now, I want to familiarize with Ubuntu 18.04 LTS and use basic Python environments.
So I followed these steps:
Created folder in my home = ~/.venvPython
(a) I think I already had a 2.7 and 3.6 by default on the OS.
(b) I do not remember for sure, I think I had to do this sudo apt-get install python3-venv.
Created environment this way after CD'ing to .venvPython folder ran this: python3 -m venv venv1BigDataPgm2
source ~/.venvPython/venv1BigDataPgm2/bin/activate
Command python --version says: Python 3.6.9
Running whereis Python shows this:
rohit#rohitUb18043LTS:~$ whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6-config /usr/bin/python2.7-config /usr/bin/python3.6m-config /usr/bin/python /usr/bin/python3.6m /usr/bin/python2.7 /usr/lib/python3.8 /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python2.7 /etc/python3.6 /etc/python /etc/python2.7 /usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/include/python3.6 /usr/include/python3.6m /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
My doubts:
Can I specify a Python version directly while creating the environment like with conda?
How do I change this to some other interpreter instead of the 3.6.9?
Do I have to manually install a different Python first, then point it somehow?
Please guide me. Thank you.
Rohit
As far as I can tell the venv standard library appeared in Python 3.3 and was never backported to 2.7.
venv can only create virtual environment for its own version of the interpreter and the virtual environment directory can not be moved to a different location or be renamed. Python 3.foo can not create a virtual environment for Python 3.bar. So it is best to pick the wanted interpreter right from the start.
Since, as shown by the output of whereis python, you seem to already have multiple Python interpreters already installed, you should be able to do something like the following:
$ /path/to/python3.3 -m venv /path/to/my/venvs/venv33
$ /path/to/python3.8 -m venv /path/to/my/venvs/venv38
There seems to be a way to change the Python interpreter associated with a virtual environment (I have not tested it, not sure what the limitations are):
$ /path/to/python3.8 -m venv --upgrade /path/to/my/venvs/venv33
Alternatively use virtualenv which seems to offer a bit more flexibility, but is probably less efficient (its next major release, virtualenv 20, should bring a lot of improvements though).
Ubuntu and other Debian-based systems generally ship whichever Python version was current and deemed sufficiently tested when the release was published; after that, only security updates which preserve the version number but add patches are released (so you might get 3.6.9-123security4 instead of 3.6.9-5 or whatever was current when the release was cut).
If you want to run a specific Python version on one of these platforms, see if you can find an Apt source which provides this version for your system (Ubuntu has a soft underbelly of unofficial PPAs of various repute; Debian has backports) or install it from source yourself. There are add-ons like pyenv which let you do this rather easily, safely, and transparently.
There may also be an existing package which gives you a particular newer version; for example, you can do apt install python3.7 and apt install python3.8 on Ubuntu 18.04, but there are no packages for 3.5 or 3.9. Try apt policy python3.7 to see which specific minor version is available from the Ubuntu package archive.
An alternative to that is to always specify the python version you wish use when running a script.
python3.6 test_script.py
Usually, when I'm on Linux and don't need a specific python3 version, I create native python3 environments.
python3 -m venv myenv
source myenv/bin/activate
But if I need a specific python3 version, I do:
python3.9 -m venv myenv
source myenv/bin/activate
To use a specific python3 version with native environments, you have to install that version using the native package manager (eg. apt).
I want to run a python script in in-built anaconda environment tensorflow_p36. To check if it is in virtual environment or not, I am using command pip -V.
My first attempt at bash script:
#!/bin/bash
source activate tensorflow_p36
python /home/ec2-user/abc/temp.py
pip -V
Note: tensorflow_p36 being an in-built environment, does not require to be called from specific /env/bin directory. It can be activated from any directory. I think it's a feature of Amazon Deep Learning AMIs.
My second attempt at bash script:
#!/bin/bash
pythonEnv="/home/ec2-user/anaconda3/envs/tensorflow_p36/"
source ${pythonEnv}bin/activate
${pythonEnv}bin/python /home/ec2-user/abc/temp.py
pip -V
Note: When I try to run source /home/ec2-user/anaconda3/envs/tensorflow_p36/bin/activate command in terminal, the environment isn't being activated.
Each time, I am getting the same result:
pip 9.0.1 from /home/ec2-user/anaconda3/lib/python3.6/site-packages (python 3.6)
Whereas, I should be getting:
pip 9.0.1 from /home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages (python 3.6)
Can someone please explain how do I activate virtual environment and run a python script from that environment? I need to use this particular environment because of the dependencies installed in it.
Extra info:
Not sure if it matters, but the tensorflow_p36 is a conda environment, not a virtualenv.
This works with virtualenv. Create environment:
virtualenv -p python 3.6 tensorflow_p36
Then change the script to:
#!/bin/bash
source $HOME/tensorflow_p36/bin/activate
python /home/ec2-user/abc/temp.py
I believe the confusion has to do with the fact that you are using anaconda and not virtualenv to create a python environment. These two tools work differently.
If you are using an EC2 instance, why not to install tensorflow_p36 globally anyway?
How can I install pygame on both python 2 and 3 on the same computer?
Its working with python 2 but i cant install it on python 3.
Use virtualenv. It will allow each project to have its own version of python and keeps all its python packages stored next to the project, rather than globally on the system.
cd into your project directory
Install a version of python 2 on your system
$ virtualenv p2env --python=2.7
Install a version of python 3 on your system
$ virtualenv p3env --python=3.5
You can how "activate" either of those environments.
$ ./p2env/bin/activate
You'll see your command prompt gets prefixed with the name of the environment. You're now running under that version of python. Additionally, any packages you pip install will be installed locally for that specific environment.
To deactivate the environment type the following.
$ deactivate
You should notice the environment name is removed from the command prompt. You're now able to switch to a new environment if you wish.
Make sure you add the environment directories virtualenv created to your .gitignore file.
/p2env
/p3env
Of course you can. Most linux distributions do so:
~$ python
python python2 python2.7 python3 python3.4 python3.4m python3m
~$ python
Debian symlinks python to python2.7 so you need to run python3 explicitly. On all other systems you can also have multiple versions in parallel in the same fashion.
Also there is a tool virtualenv which might help you in creating and manageing completely isolated python environments. (https://virtualenv.pypa.io/en/stable/)
I am experiencing an odd python thing! I can only use python 2; all other pythons (python3.4, -3.5) are gone; They exist in usr/bin, but I can't use them. For example, usr/bin/python3.4 invokes pyhton2.7. Two days ago everything was normal; I was using python3.4 just fine. All I did in last two days was to install a flask environment and I also purged teamviewer from my ubuntu.
One quick way to solve it, if you have Python 3 installed is typing python3.
Also, check this link, it probably solves your problem https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3
Check links in /usr/bin
ls -al | grep python
All python interpreters must be linked with our executable files.
Call python 3 with version, for example:
python3.4
Do not change symbolik link from python to python3, this can crash system scripts.
Install and use virtualenvwrapper:
pip install virtualenvwrapper
Check virtualenvwrapper location:
whereis virtualenvwrapper.sh
Add into you BASH config calling virtualenvwrapper and use it for creating virtual environment with python 3:
mkvirtualenv myvenv --python=/usr/bin/python3.4
Activate virtualenv and install required packages (root privilegies NOT required!):
workon myvenv
pip install flask
Call IDE from terminal with activated virtual environment for correct PATH working.
Enjoy!
Here is how I fixed it:
sudo apt-get install --reinstall python3.4
Now, everything is back to normal.
I have a virtualenv created for Python 2.5 and want to "upgrade" it to Python 2.6.
Here is how it was originally set up:
virtualenv --no-site-packages -p python2.5 myenv
I now run virtualenv in the same directory to upgrade:
virtualenv --no-site-packages -p python2.6 myenv
...
Not overwriting existing python script myenv/bin/python (you must use myenv/bin/python2.6)
...
Overwriting myenv/bin/activate with new content
The default python is still 2.5, even though I can also specify 2.6. Is there any way to remove 2.5 entirely and have 'bin/python' point to 2.6 instead?
You can use the Python 2.6 virtualenv to "revirtual" the existing directory. You will have to reinstall all the modules you installed though. I often have a virtual directory for developing a module, and virtualenv the same directory with many versions of Python, and it works just fine. :)
In Python 3.3+ venv supports --upgrade flag
--upgrade Upgrade the environment directory to use this version
of Python, assuming Python has been upgraded in-place.
Usage:
python -m venv --upgrade YOUR_VENV_DIRECTORY
I just upgraded my venv from Python 3.7.x to 3.8 on several projects without any issue.
You should create a new virtualenv using python2.6 and then, after activating the new env, use its python2.6 and its easy_install to install new versions of any site packages you need. Beware that the path name to the virtualenv is hardwired into various files within the environment, so, when you are ready to switch over to it, either change your startup scripts et al to refer to the new virualenv path or be very careful about copying it over to the old directory and modifying the path names inside it.
Install a second Python on CentOS
download python
install to diff local
configure --prefix=/opt/virtualenv/python
make && make install
create virtual env using new python
virtualenv /opt/virtualenv --python=/opt/python276/bin/python
note: if needed it can be done with a different user
chown pyuser -R /opt/virtualenv
su - pyuser
source /opt/virtualenv/bin/activate
python -v
Create virtual env:
virtualenv /opt/virtualenv
su - infograficos
source bin/activate
Install pip with python 2.7 (inside virtualenv)
easy_install pip
If you're using OS X, try this if you want to upgrade Python to a minor-increased version (e.g. 2.7.6 to 2.7.8) while keeping third-party libraries work.
It work for me on 5 different virtual environments with Django installed.
You can simply do this by go to your venv file and change the python path and it's version from pyvenv.cfg like this:enter image description here