Craig  Hicks
Craig Hicks

Reputation: 2578

Is a python virtual env supposed to be stand alone? If not, is there danger cross environment confusion?

Working in my virtual environment, any import calls not resolved in the virtual env will be resolved in the "original" environment is possible. At the same time, according to https://docs.python.org/3/tutorial/modules.html#intra-package-references , relative references are allowed. I worry this could lead to confusion/conflicts with different versions of the same named objects entering or trying to enter a programs execution. I am a novice to Python, and I am sure this issue is covered in documentation, but I can't find it. Any pointers?

Update 5/13/2017 - I found the following documention for python 3.6.1: https://docs.python.org/3/library/venv.html#creating-virtual-environments

On Windows, invoke the venv command as follows:

c:\>c:\Python35\python -m venv c:\path\to\myenv

usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] ENV_DIR [ENV_DIR ...]

--system-site-packages: Give the virtual environment access to the system site-packages dir.

The default is not to have access to the system site-packages directory. Therefore I surmise that any code outside of the system site-packages directory is considered "safe enough" with respect to usage of relative import paths, and the user is responsible for not colliding with those items.

Upvotes: 3

Views: 2575

Answers (1)

Menglong Li
Menglong Li

Reputation: 2255

This is not a big issue, try to use pyenv, and you will be able to have a stand alone python version, and if any import calls not resolved, it'll search the path of the specific python (installed by pyenv) you used to create your virtualenv instead of the sys python path.

  1. install pyenv
  2. install specific python version
  3. use the specific python version you installed by pyenv to set up your virtualenv.

to check the resolve path of your python codes

import sys
print(sys.path)

and the directories in the list will represent the order python search your path.

Upvotes: 2

Related Questions