This question already has answers here:
Use different Python version with virtualenv
(41 answers)
Closed 6 years ago.
My default python version on my Debian 8.5 machine is 3.4.2. I want to use python 2.7 for only one project. I have tried running all of the following commands in the terminal one by one, :
virtualenv -p python2.7 env_dir
virtualenv -p python2 env_dir
virtualenv --python=python2.7 env_dir
And this is the file.py inside the env_dir:
print "Hello world from inside env_dir"
Obviously however the file.py still gets executed with python3.4.2, because i run:
python file.py
and i get:
File "file.py", line 1
print "Hello world from inside env_dir"
^
SyntaxError: Missing parentheses in call to 'print'
What is the problem, why it fails to run the code in python2.7?
Update
I have also tried:
virtualenv -p /usr/bin/python2.7 env_dir
source env_dir/bin/activate
(env_dir) amir#amir-debian:~/env_dir$ python file.py
File "file.py", line 1
print "Insid virtual env"
^
SyntaxError: Missing parentheses in call to 'print'
Running python -V after activating returns: Python 3.4.2
Update-2
Here is the process how i try to create my virtualenv and the outputs from terminal:
$ virtualenv --python=/usr/bin/python2.7 venv3
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/amir/Desktop/venv3/bin/python2.7
Also creating executable in /home/amir/Desktop/venv3/bin/python
Installing setuptools, pip, wheel...done.
$ source venv3/bin/activate
(venv3) amir#amir-debian:~/Desktop$ python -V
Python 3.4.2
(venv3) amir#amir-debian:~/Desktop$ python
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Why is the working version still 3.4.2 and not 2.7 as i gave as OPTION to vertualenv when creating venv3?
Check if you have python2 by doing python -v
if you do have it, you can do python2 file.py
There's also an answer from here
which is
virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>
You can specify the version of python to use by doing
$ virtualenv venv --python=/usr/bin/python2
Related
I'm using ubuntu 22.04 and python version installed there is '3.10.4' while sqlite version is '3.37.2'. I need to use python '3.9.5' and sqlite '3.31.1' because the server that my code will live uses that.
Using pyenv fixes the problem for python but the sqlite one didn't. So I resort on building the sqlite from the source and try to link on pyenv install.
Basically this is what I tried to do:
~$ mkdir -p ~/usrapps/src
~$ cd ~/usrapps/src/
~/usrapps/src$ wget https://www.sqlite.org/2020/sqlite-autoconf-3310100.tar.gz
~/usrapps/src$ tar xvvf sqlite-autoconf-3310100.tar.gz
~/usrapps/src$ cd sqlite-autoconf-3310100/
~/usrapps/src/sqlite-autoconf-3310100$ mkdir -p ~/usrapps/sqlite3311
~/usrapps/src/sqlite-autoconf-3310100$ ./configure --prefix=/home/paulo/usrapps/sqlite3311
~/usrapps/src/sqlite-autoconf-3310100$ make
~/usrapps/src/sqlite-autoconf-3310100$ make install
~/usrapps/src/sqlite-autoconf-3310100$ cd ~/
Here's the full terminal ouput
Now for installation of python 3.9.5, I do this different scenarios but first, brew-pyenv-i is aliased to CC="$(brew --prefix gcc)/bin/gcc-12" pyenv install the reason is I encounter a issue where installing python result to "BUILD FAILED: Mising OpenSSL" even though I have openssl install. Doing this fixed my issue, here's the reference.
I uninstall first python(installed by pyenv) before each scenarios.
scenario 1 - log:
~$ LDFLAGS="-L/home/paulo/usrapps/sqlite3311/lib" CPPFLAGS="-I/home/paulo/usrapps/sqlite3311/include" brew-pyenv-i 3.9.5
scenario 2 - log:
~$ PYTHON_CONFIGURE_OPTS="LD_RUN_PATH=/home/paulo/usrapps/sqlite3311/lib LDFLAGS=-L/home/paulo/usrapps/sqlite3311/lib" brew-pyenv-i 3.9.5
scenario 3 - log:
~$ PYTHON_CONFIGURE_OPTS="LD_RUN_PATH=/home/paulo/usrapps/sqlite3311/lib LDFLAGS=-L/home/paulo/usrapps/sqlite3311/lib CPPFLAGS=-I/home/paulo/usrapps/sqlite3311/include" brew-pyenv-i 3.9.5
All attempts are successfully installed the python 3.9.5, but when I try check the sqlite version, its still same, not '3.31.1'
~$ pyenv shell 3.9.5
~$ python
Python 3.9.5 (default, Aug 26 2022, 08:45:14)
[GCC 12.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.37.2'
I would like to avoid using pysqlite3, because that's mean I have to rewrite some of my code and my goal is not really to use multiple versions of sqlite in my app.
I have created a virtual environment (directory is env) and have installed some third party packages. I work with Mac OSX. How can I activate my venv in the python interactive shell? I tried
source env/bin/activate
and get the error message: SyntaxError: invalid syntax
I have seen some had posted this question 8 years ago but I haven't found what I was looking for...I have also checked the python documentation but it is not clear for me how to activate it
https://docs.python.org/3/tutorial/venv.html?highlight=virtual
Can anybody please help?
In your shell, normally python will point you to a default instance of python:
python
Python 2.7.14 (default, Sep 25 2017, 09:53:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
This is because python is located in /usr/local/bin which is part of your default PATH. Now, when you source activate <some_env>, you are modifying PATH, which you can actually see in the /path/to/venv/some_env/bin/activate file:
#!/bin/bash
...
if [[ $PATH == *"CONDA_PATH_PLACEHOLDER"* ]]; then
# If it did, replace it with our _NEW_PART
export PATH="$($_CONDA_PYTHON -c "import re; print(re.sub(r'CONDA_PATH_PLACEHOLDER', r'$_NEW_PART', '$PATH', 1))")"
else
#### HERE IS WHERE THAT PATH IS OVERWRITTEN
export PATH="$_NEW_PART:$PATH"
fi
# CONDA_DEFAULT_ENV is the shortest representation of how conda recognizes your env.
# It can be an env name, or a full path.
# Last date of change: 2016-06-21
# If the string contains / it's a path
if [[ "$#" == */* ]]; then
export CONDA_DEFAULT_ENV=$(get_abs_filename "$args")
else
export CONDA_DEFAULT_ENV="$args"
fi
...
esac
I'm using anaconda but the concept is the same, and ... indicates where I've snipped out parts of the script. It is effectively exporting the venv python executable path into PATH. So you won't be pointing to your venv python unless one of two conditions arises: a) you explicitly call that python executable or b) you source activate myenv and then call python.
So the correct workflow is:
source /path/to/myenv/bin/activate myenv
Then call
python
As an example, the first call to $PATH is before activating my conda env, the second is after:
➜ ~ echo $PATH
/Users/mm92400/bin:/usr/local/bin:/Users/mm92400/anaconda3/bin:/Users/mm92400/.cargo/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands
➜ ~ source activate py36
(py36) ➜ ~ echo $PATH
/Users/mm92400/anaconda3/envs/py36/bin:/Users/mm92400/bin:/usr/local/bin:/Users/mm92400/anaconda3/bin:/Users/mm92400/.cargo/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands
note how $PATH is different
I'm doing a learn python the hardway tutorial, and they are using python2.7
I got it downloaded but unable to switch back from 3.3 to 2.7
I manipulated PATH variable, adding C:\Python27 but this was no use
any other suggestion?
Rename the python interpreter executables to their respective versions. The OS is just executing the first 'python' executable it finds in the path, which is probably the 3.x version. So in command line, you can type python2 or python3 to select the version of interpreter you want.
Another option is.
you can create virtual environment for python 2.7 version.
And Activate the environment.
And use your virtual env for your python 2.7 learning.
username#mypc:~/dev/learn-code$ virtualenv myenv -p /usr/bin/python
Already using interpreter /usr/bin/python
New python executable in /home/username/dev/learn-code/myenv/bin/python
Installing setuptools, pip, wheel...done.
username#mypc:~/dev/learn-code$
username#mypc:~/dev/learn-code$
username#mypc:~/dev/learn-code$ source myenv/bin/activate
(myenv) username#mypc:~/dev/learn-code$
(myenv) username#mypc:~/dev/learn-code$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
>>>
Setting up in windows
environment also similar. see this link
1) I installed python3.5 on windows with the installer. I checked set to path.
2) I installed python2.7 on windows with the installer. I checked set to path.
python 3 is found in C:\Users\amand\AppData\Local\Programs\Python\Python35-32
python 2 is found in C:\Python27
3) I open the command prompt
>>> python -m venv tutorial-env
C:\Python27\python.exe: No module named venv
Okay....so how about...
>>>py -3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>python -m venv tutorial-env
>File "<stdin>", line 1
python -m venv tutorial-env
^
SyntaxError: invalid syntax
Okay. I closed my command prompt and started a new one. The document said to use this...but...
>>>>python3 -m venv tutorial-env
'python3' is not recognized as an internal or external command,
operable program or batch file.
Documentation that I am referring to
https://docs.python.org/3/tutorial/venv.html
to use a specific python you can do:
python3 -m venv tutorial-env #for python 3
python2 -m venv tutorial-env #for python 2
#if you have multple versions of 3 or 2
python2.7 -m venv tutorial-env #for python 2.7
python3.5 -m venv tutorial-env #for python 3.5 or something along the lines
EDIT
If in Windows and the aboves not working:
#In command prompt
py -3 #or -2
>>> import venv
>>> venv.main('tutorial-env')
or
py -3 -m venv tutorial-env
I have a Linux drive with no root access. It has Python 2.4.2 installed in /usr/bin/python directory.
which python
/usr/bin/python
I installed Python 2.7.8 in my local folder:
cd /usr2/steve/bin
ls
python2.7
If I still run Python it starts older version:
python
Python 2.4.2 (#1, May 6 2011, 13:26:21)
[GCC 4.1.2 20070115 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I have added /usr2/steve/bin to the PATH:
echo $PATH
/usr2/steve/usr2/steve/bin:/usr2/steve/local/mnt/workspace/steve/Python-2.7.8:/usr2/steve/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
How do I force the system to use the newer Python version as opposed to the older one?
You only have a python2.7 binary in your /usr2/steve/bin folder. You can make a symlink, then just calling python should work:
cd /usr2/steve/bin
ln -s python2.7 python
make your life simple by creating an alias. Are you using BASH (hopefully you are)? If so this is the syntax
alias pythonsteve='/usr2/steve/bin/python2.7'
(drop the = sign if you are running CSH). Next, run to verify
$pythonsteve -c 'import sys; print sys.prefix'
/usr2/steve/bin
Put the alias command in your shell user start-up file. On typical BASH installs this is the file ~/.bashrc. Note, when you manually edit the ~/.bashrc file you must either log out/log in, or run source ~/.bashrc for the commands to execute.
If you want to use python 2.7 you can specify it by appending it after 'python'
which python2.7