Python3 with virtualenv - python

I am trying to set up a python3 virtualenv on my mac.
I type in my folder :
virtualenv -p python3
source bin/activate
And is says :
Running virtualenv with interpreter /usr/local/bin/python3
However when I do print(5/2) it outputs 2, which is python2. I conclude that my virtualenv isn't running on python3 but on python2. What am I doing wrong ?
Thanks a bunch

Related

Virtual environment doesn't work on Cloud9

Cloud9 (an online ide) doesn't seem to support my virtual environment:
me:~/workspace/dir (master) $ source venv/bin/activate
(venv) me:~/workspace/dir (master) $ which python
/usr/bin/python
This same virtual directory worked fine on my local machine:
(venv) me$ which python
/Users/me/dir2/dir/venv/bin/python
How can I fix this?
The following works for me.
sudo apt-get install python3.5-venv
python3.5 -m venv --clear ./mypy3.5/
source ./mypy3.5/bin/activate
It uses the
(mypy3.5) $ which python
/home/ubuntu/mypy3.5/bin/python
But there is a gotcha which might have been your problem. The python3 -m venv uses soft links to how your python resolves in your environment. I had Python 3.3, 3.4 and 3.5 installed in /usr/local so the /usr/local/bin/python3 would change and break my Python3 venv. Note that "python3" is evaluated for the environment not for an absolute path. To be careful, when there are more than one Python 3 on you system, create your virtual environment with an explicit path like the following.
/usr/bin/python3.5 -m venv --clear ./mypy3.5/
source ./mypy3.5/bin/activate
ls -l $(which python3.5)
/home/ubuntu/mypy3.5/bin/python3.5 -> /usr/bin/python3.5*

How to create a Python 3.5 virtual environment with Python 2.7?

My system is running CentOS 6. I do not have admin access, so sudo is not available. I have Python 2.7.3 available, along with pip and virtualenv. I was hoping that I could use these to set up a new virtual environment in which to install & run Python 3.5 or above.
I tried the method described here:
Using Python 3 in virtualenv
But got this error:
$ virtualenv -p python3 venv
The path python3 (from --python=python3) does not exist
My system also has a Python 3.4 module installed, so I tried that, however virtualenv does not seem to work there:
$ module load python/3.4.3
$ virtualenv -p python3 venv
-bash: virtualenv: command not found
This appears to make sense since virtualenv is only installed for Python 2.7:
$ module unload python
$ module load python/2.7
$ which virtualenv
/local/apps/python/2.7.3/bin/virtualenv
So, the next logical step would seem to be installing virtualenv for my Python 3... but this does not work either:
$ pip3 install virtualenv
Traceback (most recent call last):
File "/local/apps/python/3.4.3/bin/pip3", line 7, in <module>
from pip import main
ImportError: cannot import name 'main'
also
$ pip3 install --user virtualenv
Traceback (most recent call last):
File "/local/apps/python/3.4.3/bin/pip3", line 7, in <module>
from pip import main
ImportError: cannot import name 'main'
I started Google'ing this new error message, but did not see anything that seemed relevant for this situation. Any ideas? Even if I could get virtualenv installed on my Python 3.4 module, would I still be unable to upgrade it to Python 3.5+?
To round things out, I also tried to compile my own Python 3.6 from source, but that does not work either:
Python-3.6.0$ make install
if test "no-framework" = "no-framework" ; then \
/usr/bin/install -c python /usr/local/bin/python3.6m; \
else \
/usr/bin/install -c -s Mac/pythonw /usr/local/bin/python3.6m; \
fi
/usr/bin/install: cannot create regular file `/usr/local/bin/python3.6m': Permission denied
make: *** [altbininstall] Error 1
more background info:
$ which pip3
/local/apps/python/3.4.3/bin/pip3
$ which python
/local/apps/python/3.4.3/bin/python
You can download miniconda or Anaconda. It does not require superuser privileges because it installs in your home directory. After you install you can create new environments like this:
conda create -n py35 python=3.5
Then you can switch to the new environment:
source activate py35
Try this for Windows.
virtualenv -p C:\Python35\python.exe django_concurrent_env
cd django_concurrent_env
.\Source\activate
deactivate
Try out the following commands:
pip3 install virtualenv
pip3 install virtualenvwrapper
mkdir ~/.virtualenvs
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bash_profile
which python3
Now copy the result of path of python3 in the last command and put it in the following command:
mkvirtualenv --python=python3/path/in/last/command myenv
I'm assuming pip3 is already installed. If not, install it before running these commands.
Source: https://docs.coala.io/en/latest/Help/MAC_Hints.html#create-virtual-environments-with-pyvenv
(I do have sudo access on my machine. I've not tried the commands without it. Please post if any issues comes.)
Since you already have virtualenv installed, you might only need to update the files and then run the command mkvirtualenv with proper arguments.

A strange virtualenv issue-- even with -p it installs multiple pythons

I'm trying to create a virtual environment with 3.4. I have followed instructions found through searching stackoverflow, as well as posting this issue on reddit, and continue to have the same problem: even when I specify python3.4, it installs both 3.4 AND 2.7. This then leads to all of the problems I was trying to avoid by using a virtual environment in the first place.
$ which python3
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3
$ mkdir test
$ cd test
$ virtualenv -p /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 venv
(various output saying it's creating venv/bin/python with 3.4)
$ source venv/bin/activate
(venv)$ python --version
Python 2.7.9
when I look in the venv/bin folder it has binaries for both 2.7.9 and 3.4.
after activating the environment:
$ echo $PATH
/Users/theinevitable/Documents/python/test/venv/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Applications/Postgres.app/Contents/Versions/9.4/bin
$ which python
/Users/theinevitable/Documents/python/test/venv/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3
one person on reddit suggested trying to use venv instead:
$ python3.4 -m venv my_venv
/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4: No module named venv
$ pyvenv tester
File "/Library/Frameworks/Python.framework/Versions/3.4/bin/pyvenv", line 10
print('Error: %s' % e, file=sys.stderr)
^
SyntaxError: invalid syntax

Installing distribute in Python 3.3 venv (OS X/Homebrew)

I've been trying to get up and running with the built-in "venv" module of Python 3.3 on my OS X machine. I've installed Python 3.3 using Homebrew.
As per the docs, creating and switching virtual environment works as you'd expect:
$ python3 -m venv myvenv
$ source myvenv/bin/activate
And I've tested something like this:
$ echo "YEAH = 'YEAH!'" > myvenv/lib/python3.3/site-packages/thingy.py
$ python
>>> import thingy
>>> print(thingy.YEAH)
'YEAH!'
But when I try to install distribute, it simply won't go in the proper place. For some reason, it insists on trying to install into /usr/local/lib/python3.3/site-packages/, which fails with the following messages:
No setuptools distribution found
running install
Checking .pth file support in /usr/local/lib/python3.3/site-packages/
/Users/victor/myvenv/bin/python -E -c pass
TEST FAILED: /usr/local/lib/python3.3/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from. The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/usr/local/lib/python3.3/site-packages/
and your PYTHONPATH environment variable currently contains:
''
This happens regardless if I try to install using distribute_setup.py or using the source distribution directly. I've even tried using --prefix=/Users/victor/myenv but it still tries to put everything in my "global" site-packages.
I can't figure out why this happens, but it's consistent on two of my machines. Note that sys.prefix reports the correct path (the virtual environment).
Is this a problem with Homebrew? OS X? Python 3.3? venv? Me?
This has been an issue with Homebrew, yes, but it is working now since https://github.com/mxcl/homebrew/commit/0b50110107ea2998e65011ec31ce45931b446dab.
$ brew update
$ brew rm python3 #if you have installed it before
$ brew install python3
$ cd /tmp
$ which python3
/usr/local/bin/python3
$ python3 -m venv myvenv
$ source myvenv/bin/activate
$ wget http://python-distribute.org/distribute_setup.py # may need brew install wget
$ python3 distribute_setup.py
...
Finished processing dependencies for distribute==0.6.45
After install bootstrap.
Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info
Creating /private/tmp/myvenv/lib/python3.3/site-packages/setuptools.pth
You see that distribute install successfully into the /tmp dir.
This happens because homebrew installs distutils config file:
$ brew cat python3 | grep "Tell distutils" -A5
# Tell distutils-based installers where to put scripts
(prefix/"Frameworks/Python.framework/Versions/#{VER}/lib/python#{VER}/distutils/distutils.cfg").write <<-EOF.undent
[install]
install-scripts=#{scripts_folder}
install-lib=#{site_packages}
EOF
$ mv ~/.local/Frameworks/Python.framework/Versions/3.3/lib/python3.3/distutils/distutils.cfg ~/tmp/
$ cat ~/tmp/distutils.cfg
[install]
install-scripts=/Users/gatto/.local/share/python3
install-lib=/Users/gatto/.local/lib/python3.3/site-packages
$ . venv/bin/activate
(venv) $ python distribute-0.6.36/distribute_setup.py
(venv) $ ls venv/lib/python3.3/site-packages/
distribute-0.6.36-py3.3.egg easy-install.pth setuptools-0.6c11-py3.3.egg-info setuptools.pth
See "distutils.cfg Can Break venv" issue at bugs.python.org.

How come I can not activate my Virtual Python Environment with 'source env/bin/activate' command?

I am trying to activate my Virtual Python Environment to use with Pylons but I think I am executing the commands wrong.
jem#jem-laptop:~$ source env/bin/activate
bash: env/bin/activate: No such file or directory
What am I doing wrong?
How should I do it right?
I realize I had to do
jem#jem-laptop:~$ ls
Desktop examples.desktop Public shortener.rb
Documents Mac4Lin_v1.0 ruby-1.9.1-p378 Templates
Downloads Music rubygems-1.3.7 Videos
Dropbox Pictures setcolors.vim virtualenv.py
And here we see virtualenv.py. From here I just had to
jem#jem-laptop:~$ virtualenv ENV
New python executable in ENV/bin/python
Installing setuptools............done.
And then
jem#jem-laptop:~$ source ENV/bin/activate
(ENV)jem#jem-laptop:~$ deactivate
jem#jem-laptop:~$
Solved :)
I usually do it this way:
$ cd the_project_dir
$ . bin/activate
(the_project)$ _
I need to be in the project directory anyway to go on with the work.
Obviously the_project_dir is the name of a directory where you have created a virtualenv.
In 2.7 version I used this command:
$ cd project_name
$ virtualenv venv --distribute
$ source venv/Scripts/activate
(venv)
I would recommend using virtualenvwrapper. It makes working with virtualenv a lot simpler, especially if you have more than one virtualenv.
Simple fix:
$ virtualenv env
$ cd env/Scripts/
$ . activate
On FreeBSD I solved this doing following:
# ls mypienv
# mypienv/bin/activate
mypienv/bin/activate: Permission denied.
# chmod +x mypienv/bin/activate
# mypienv/bin/activate
Missing '}'.
And you see that script not working but:
# ls mypienv/bin/
activate activate.fish easy_install-2.7 pip2.7 python2
activate_this.py activate.ps1 pip python python2.7
activate.csh easy_install pip2 python-config wheel
Finaly:
# python mypienv/bin/activate_this.py
And it worked!
P.S. I am new with python python verions 2.7
env/Scripts/activate worked for me.
For Windows, the following worked for me:
C:\.virtualenvs\env\Scripts>activate.bat

Categories

Resources