How to create virtual environment for python 3.7.0?

半城伤御伤魂 提交于 2020-12-01 07:43:33

问题


I'm able to install it with root user but I wanted to install it in a clean environment. My use case is to test the installation of another application with pip for the customer who is using python3.7.0

sudo apt-get update

sudo apt-get install build-essential libpq-dev libssl-dev openssl libffi-dev zlib1g-dev

sudo apt-get install python3-pip python3-dev

sudo apt-get install python3.7

Thanks.


回答1:


(assuming python3.7 is installed)

Install virtualenv package:

pip3.7 install virtualenv

Create new environment:

python3.7 -m virtualenv MyEnv

Activate environment:

source MyEnv/bin/activate



回答2:


To help anyone else who runs into the chicken & egg situation trying to use the above chosen answer, here's what solved it for me:

sudo apt install python3.7-venv
python3.7 -m venv env37
source env37/bin/activate
deactivate (when done using the environment)

I had installed python 3.7 using deadsnakes vs source:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7

In doing so I could run python3.7 --version but since I had no pip3.7 I could not install virtualenv as directed in the solution above. Luck would have it that deadsnakes has venv! Once I installed venv I could create my environment & be on my merry way

Handy official python page with venv info

So why didn't I use?: python3.7 -m ensurepip

That was giving me:

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/lib/python3.7/dist-packages/easy_install.py' Consider using the --user option or check the permissions.

Which left me with 3 choices:

use sudo (which is simple but I keep being told is frowned upon) install with --user option which wasn't ideal in that I may not always be logged in as the same user or install it in an environment which I'm told is the recommended route.

But see chicken egg above.. How do I install pip in environment when I can't create venv or virtualenv? Thus my workaround solution of installing venv from deadsnakes which allowed me to create the virtual environment to then install pip3.7:

(env37) user@ubuntu:~$ python3.7 -m ensurepip
(env37) user@ubuntu:~$ pip3.7 --version
pip 19.2.3 from /home/user/env37/lib/python3.7/site-packages/pip (python 3.7)



回答3:


Using pip on windows, you can do the following:

1.virtualenv --python "C:\\Python37\\python.exe" venv# use your own path

You will see something like this:

Running virtualenv with interpreter C:\Python37\python.exe Using base prefix 'C:\Python37' New python executable in C:\Users\XXXX\Documents\GitHub\MyProject\venv\Scripts\python.exe Installing setuptools, pip, wheel... done.

2.C:\Users\XXXXX\Documents\GitHub\MyProject>cd venv C:\Users\XXXXX\Documents\GitHub\MyProject\venv>cd Scripts C:\Users\XXXXX\Documents\GitHub\MyProject\venv\Scripts>activate.

At the beginning of the command path, when you see (environment variable name) in this case (venv), this is a sign that your virtual environment is activated. (venv) C:\Users\tuscar2001\Documents\GitHub\MyProject\venv\Scripts>

Please check the following link for more details:http://www.datasciencetopics.com/2020/03/how-to-set-up-virtual-environment-in.html



来源:https://stackoverflow.com/questions/52816156/how-to-create-virtual-environment-for-python-3-7-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!