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*
Related
I setup Python 3.6 using pyenv so I could manage multiple Python versions (e.g. 3.7 and 3.8) in the future. I didn't use Homebrew to install Python since it changes the system version. It's my first time to use zsh shell since it's the default shell in Catalina OS. Currently, I use 3.6.8 version for my existing project.
So here's my current setup:
% pyenv versions
result:
system
* 3.6.8 (set by /Users/macbook/.python-version)
3.7.3
% python -V results to Python 3.6.8
% which python results to /Users/macbook/.pyenv/shims/python
% echo $PATH results to /Users/macbook/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
The content of my ~/.zshrc is PATH=$(pyenv root)/shims:$PATH
I created a virtual env using % python -m venv venv, installed all the necessary packages, and when I activate it and get the python path,
(venv) % python -V
Python 3.6.8
(venv) % which python
/Users/macbook/python-project/venv/bin/python
(venv) % echo $PATH
/Users/macbook/python-project/venv/bin:/Users/macbook/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Finally, when I try to run the app, I always get zsh: abort error:
(venv) % python app.py
zsh: abort python app.py
(venv) % export FLASK_APP=app.py
(venv) % flask run
zsh: abort flask run
I don't know what else is still missing or are there anything wrong with my python path?
Thanks!
After searching through the web, I think this is a common issue with the latest MacOS or Homebrew. This thread fixed the issue.
brew update && brew upgrade && brew install openssl
copy the two files from /usr/local/Cellar/openssl#1.1/1.1.1g to /usr/local/lib/
cd /usr/local/Cellar/openssl#1.1/1.1.1g/
sudo cp libssl.1.1.1.dylib libcrypto.1.1.1.dylib /usr/local/lib/
add symlink to missing openssl libs
cd /usr/local/lib
sudo ln -s libssl.1.1.1.dylib libssl.dylib
sudo ln -s libcrypto.1.1.1.dylib libcrypto.dylib
For me, the below worked:
Python 3.6.9
MacOs Catalina 10.15.7
cd /usr/local/Cellar/openssl#1.1/1.1.1h/
cp lib/libssl.1.1.dylib lib/libcrypto.1.1.dylib /usr/local/lib
cd /usr/local/lib
sudo ln -s libssl.1.1.dylib libssl.dylib
sudo ln -s libcrypto.1.1.dylib libcrypto.dylib
Thanks to the answer by Zhanrah
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
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
I am trying to create a virtual environment for Python 3.4 on a fresh install of Ubuntu Server 14.04. I following the instructions for the venv module at:
https://docs.python.org/3/library/venv.html#module-venv
I don't have a lot of Python 3.4 or Ubuntu experience.
When I type the command:
pyvenv testDir
I get back:
pyvenv: command not found
What is causing this?
Ubuntu 14.04 uses Python 2 by default, and the pyenv command does not exist in Python 2 out of the box.
You can, however, use virtualenv for the same purpose. You just need to install it!
You should:
Install Python 3 and virtualenv apt-get install -y python3 python-virtualenv
Create a Python 3 virtualenv: virtualenv -p $(which python3) testDir
Activate the virtual environment with source testDir/bin/activate
It's also possible to create virtualenv by python itself.
python3 -m venv myenv
see documentation https://docs.python.org/3/library/venv.html
It's in the package python3.4-venv (Linux Mint) or python3-venv (Ubuntu - I guess).
The advantages of venv over virtualenv are that (1) it's in vanilla Python3, (2) interpreter does retain tab-completion.
Edit the .bashrc file present in your home directory by adding below code and save the file:
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="/home/'Enter systemname here'/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
After this you can now run the following command:
exec $SHELL
Now pyenv works properly
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.