How to Create Virtual Environment for Python 3.7.0

How to create virtual environment for python 3.7.0?

(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

Using Python 3 in virtualenv

simply run

virtualenv -p python3 envname

Update after OP's edit:

There was a bug in the OP's version of virtualenv, as described here. The problem was fixed by running:

pip install --upgrade virtualenv

How to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?

I don't know if it's best practices or not, but if I also install python3-venv and python3.7-venv then everything works (this is tested on a fresh stock Debian buster docker image):

% sudo apt install python3.7 python3-venv python3.7-venv
% python3.7 -m venv py37-venv
% . py37-venv/bin/activate
(py37-venv) %

Note that it also installs all of python3.6 needlessly, so I can't exactly say I like it, but at least it does work and doesn't require running an unsigned script the way get-pip.py does.

Python3.7 venv does not Create Virtual Environment Directory

It should work, I tested it several times (e.g.: [SO]: PyWin32 (226) and virtual environments).
And yes, they are 2 different kinds of animals:

  • [Python 3.Docs]: venv - Creation of virtual environment
  • [PyPA.VirtualEnv]: Virtualenv.

Example:

e:\Work\Dev\StackOverflow\q059885771>sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> set PY
PYTHONPATH=e:\Work\Dev\Utils\current

[prompt]> dir /b

[prompt]> "c:\Install\pc064\Python\Python\03.07.06\python.exe" -c "import sys, venv;print(sys.version);print(venv)"
3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]
<module 'venv' from 'c:\\Install\\pc064\\Python\\Python\\03.07.06\\lib\\venv\\__init__.py'>

[prompt]> "c:\Install\pc064\Python\Python\03.07.06\python.exe" -m venv
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
[--upgrade] [--without-pip] [--prompt PROMPT]
ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR

[prompt]> time<nul
The current time is: 21:51:57.12
Enter the new time:
[prompt]> "c:\Install\pc064\Python\Python\03.07.06\python.exe" -m venv ".\venv_dir"

[prompt]> echo %errorlevel%
0

[prompt]> time<nul
The current time is: 21:52:10.54
Enter the new time:
[prompt]>
[prompt]> dir /b
venv_dir

[prompt]> "venv_dir\Scripts\python.exe"
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z


[prompt]>

Notes (things to pay attention to):

  • Make sure that things are actually what you think they are: use full paths (like I did for the Python executable). To see what interpreter is invoked in your case, type where python3 in the console

    • Might also apply this to the environment directory
  • Enclose paths in dblquotes ("${SOME_PATH}"), as they might contain spaces (and the command interpreter will interpret the part after the SPACE as an argument to the one before)
  • Check the command return code
  • Check environment variables (e.g.: PYTHONPATH) that might impact the behavior
  • Of course, Python should be valid (no errors encountered during installation, no files deleted / modified afterwards, ...)

When all the above checks pass, you should be able to create and use a (venv created) virtual environment.

Update #0

I took a look at the URL added in the question: [SuperUser]: Typing “python” on Windows 10 (version 1903) command prompt opens Microsoft store (mentioning @Update, @TusharGautam, @MattJecha, @Ramhound great answers, containing useful info and resources).

Apparently, it is because of the 2 App Installer related items from App execution aliases. Then I went further, and also installed Python 3.7 from Microsoft Store.
Both (App Installer and Python 3.7) place some items (including executables) in "%USERPROFILE%\AppData\Local\Microsoft\WindowsApps". The funny thing is that they are 0 bytes sized, and they are neither shortcuts nor symlinks. I tend to think they don't actually exist on the filesystem, but are some kind of "mock entries" that Win displays and acts as if they would be real (the same thing MS did when displaying the .NET assemblies (which are basically .dll files) as directories in Windows Explorer).

In the image below, I chose the 2 boxed items as examples:

Img0

When checking one, the other gets automatically unchecked, which makes sense as both point to the same "executable" (python3.exe).

[prompt]> :: Python3.7 (python3.exe - installed from Microsoft Store) checked
[prompt]> dir "c:\Users\cfati\AppData\Local\Microsoft\WindowsApps\py*"
Volume in drive C is SSD0-WIN
Volume Serial Number is F2CE-FA29

Directory of c:\Users\cfati\AppData\Local\Microsoft\WindowsApps

20/01/24 12:32 0 python3.exe
20/01/24 12:12 <DIR> PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
1 File(s) 0 bytes
1 Dir(s) 197,737,488,384 bytes free

[prompt]> python3 -c "import sys, os;print(\"VER: {0:}\nEXE: {1:}\nCWD: {2:}\nPyPATH: {3:}\".format(sys.version, sys.executable, os.getcwd(), sys.path))"
VER: 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 01:54:44) [MSC v.1916 64 bit (AMD64)]
EXE: C:\Users\cfati\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe
CWD: e:\Work\Dev\StackOverflow\q059885771
PyPATH: ['', 'e:\\Work\\Dev\\Utils\\current', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\python37.zip', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\lib', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1776.0_x64__qbz5n2kfra8p0\\lib\\site-packages']

[prompt]> echo %errorlevel%
0

[prompt]>
[prompt]> :: App Installer (python3.exe) checked
[prompt]> dir "c:\Users\cfati\AppData\Local\Microsoft\WindowsApps\py*"
Volume in drive C is SSD0-WIN
Volume Serial Number is F2CE-FA29

Directory of c:\Users\cfati\AppData\Local\Microsoft\WindowsApps

20/01/24 12:35 0 python3.exe
20/01/24 12:12 <DIR> PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
1 File(s) 0 bytes
1 Dir(s) 197,737,291,776 bytes free

[prompt]> python3 -c "import sys, os;print(\"VER: {0:}\nEXE: {1:}\nCWD: {2:}\nPyPATH: {3:}\".format(sys.version, sys.executable, os.getcwd(), sys.path))"
Access is denied.

[prompt]> :: !!! "This app can't run on your PC" popped up !!!
[prompt]> echo %errorlevel%
5

As a note, the executable ("C:\Users\cfati\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe") is also a "mock entry", the real one is actually located in "%ProgramFiles%\WindowsApps"

From [MS.DevBlogs]: Who put Python in the Windows 10 May 2019 Update? (emphasis is mine):

While Python continues to remain completely independent from the operating system, every install of Windows will include python and python3 commands that take you directly to the Python store page.

So, there you go, python3 is not actually python3 (of course, appplies to python as well). The funny thing is that in your case it "worked", while in mine it complained (ERROR_ACCESS_DENIED).

Conclusions:

  • I must insist on always using the full paths. That way you are not impacted by changes happening in the OS (well, your files might get deleted or corrupted, but then you'd have bigger issues)
  • Specifying the full path every time, would soon become very annoying, that's why the PATH environment variable could / should be used. Of course, this works fine (and significantly improves the experience) in probably 90+% of the cases, but there are some (for example when other installed apps / tools that also ship Python, add their dirs into PATH) where it can trigger funny results

error when creating a pipenv virtual environment with python 3.7

Did you install the python3-distutils package ?
If not you can install it with :

 sudo apt-get install python3-distutils

If you need it for a python3 version that is not the system default, specify the python 3 version :

sudo apt-get install python3.X-distutils

example: python3.8-distutils

Problems setting up a python 3.7 virtual environment

The problem was that I renamed the python exe's. I don't know exactly what goes wrong, but presumably at some point venv tries to find python.exe and is thrown off by the name.

Changing them back to python.exe and differentiating between the versions with their location fixed the problem.

Edit:
Check out Eryk's comments for more details.

How to Install pip for python 3.7 on Ubuntu 18?

In general, don't do this:

pip install package

because, as you have correctly noticed, it's not clear what Python version you're installing package for.

Instead, if you want to install package for Python 3.7, do this:

python3.7 -m pip install package

Replace package with the name of whatever you're trying to install.

Took me a surprisingly long time to figure it out, too. The docs about it are here.

Your other option is to set up a virtual environment. Once your virtual environment is active, executable names like python and pip will point to the correct ones.



Related Topics



Leave a reply



Submit