2017-04-16 96 views
1

我使用的是Ubuntu 16.04,我總是使用python3 -m vevn venv來創建我的Python的虛擬環境,並使用source venv/bin/active來激活它。如何讓Python3的venv使用與我的系統相同版本的pip3?

我已經確定我的全局系統環境的pip3是最新版本,例如9.0.1,但每次我使用上述命令創建虛擬環境時,初始venv的pip3版本始終比如舊的,例如8.1.1,這讓系統提醒我每次升級我的pip3。

我試過sudo apt-get install --upgrade python3-venv在我的系統環境中,但一切都在最新版本。

我該如何讓Python3的venv使用與我的系統相同版本的pip3,這樣我不必每次創建虛擬環境時都升級我的pip3? venv的pip3的版本如何決定?

謝謝。

回答

0

關於pyvenvvenv的前身有一個類似的question。 這裏的答案的相關部分:

Ensurepip包將無法從互聯網上下載或從其他地方抓取文件,因爲所有必需的組件都已經納入了包。這樣做會增加安全漏洞,因此不受支持。

包不會像pip那樣經常更新。

我解決了建立與下面的命令熱鍵問題:

$ python3 -m venv venv && source venv/bin/activate && pip3 install --upgrade pip

0

我設法在我的Windows上安裝最新版本。但是這種方法可能會導致安全問題在嘗試這個之前,請注意你在做什麼

我只在Windows上的Python 3.5.2上測試過它。如果您使用Python2.x,最好不要嘗試它。

首先,要弄清楚你的venv使用什麼版本。用途:

>>> import inspect 
>>> import venv 
>>> print(inspect.getsource(venv)) 
[the source of venv goes here] 

而且你會發現,在功能_setup_pip的認定中:

def _setup_pip(self, context): 
    """Installs or upgrades pip in a virtual environment""" 
    # We run ensurepip in isolated mode to avoid side effects from 
    # environment vars, the current directory and anything else 
    # intended for the global Python environment 
    cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade', 
               '--default-pip'] 
    subprocess.check_output(cmd, stderr=subprocess.STDOUT) 

此功能告訴我們,PIP是叫ensurepip另一個Python包安裝。所以我們會去的ensurepip源使用:

>>> import ensurepip 
>>> print(inspect.getsource(ensurepip)) 
[the source of ensure pip goes here] 

在源的前幾行,你將獲得:

_SETUPTOOLS_VERSION = "20.10.1" 
_PIP_VERSION = "8.1.1" 

這是版本信息的ensurepip捆綁。但只有更改這兩行纔會導致失敗,因爲您沒有更改本地.whl軟件包。那麼在哪裏。whl包位於?這,源裏面的功能bootstrap

def bootstrap(*, root=None, upgrade=False, user=False, 
       altinstall=False, default_pip=False, 
       verbosity=0): 
    ... 
    with tempfile.TemporaryDirectory() as tmpdir: 
    # Put our bundled wheels into a temporary directory and construct the 
    # additional paths that need added to sys.path 
    additional_paths = [] 
    for project, version in _PROJECTS: # *pay attention to these lines* 
     wheel_name = "{}-{}-py2.py3-none-any.whl".format(project, version) 
     whl = pkgutil.get_data(
      "ensurepip", 
      "_bundled/{}".format(wheel_name), 
     ) 
     with open(os.path.join(tmpdir, wheel_name), "wb") as fp: 
      fp.write(whl) 

     additional_paths.append(os.path.join(tmpdir, wheel_name)) 
    ... 

注意什麼我評論就行了。因此,這告訴我們,位於ensure/_bundled/目錄中的.whl文件,您也可以使用inspect.getsourcefile(ensurepip)。我得到的是'c:\\python35\\lib\\ensurepip\\__init__.py'

轉到此目錄,您有兩個文件:pip-8.1.1-py2.py3-none-any.whl,setuptools-20.10.1-py2.py3-none-any.whl

我上面寫的只是讓你瞭解我們在做什麼,所以以下是你需要做什麼

  1. 轉到/your/python/lib/dir/ensurepip/_bundled/
  2. 下載使用命令PIP輪文件,我得到了pip-9.0.1-py2.py3-none-any.whl

    $ pip download pip

  3. 編輯的ensurepip源。在ensurepip/__init__.py,編輯這樣並將其保存:

    # _PIP_VERSION = "8.1.1" # comment this line 
    _PIP_VERSION = "9.0.1" # add this line 
    
  4. 現在你做到這一點。要檢查PIP版本:

    $ python -m venv testpip 
    $ cd testpip 
    $ .\Scripts\activate.bat (in Linux, use '$ source ./bin/activate' instead.) 
    (testpip) $ pip --version 
    pip 9.0.1 from the \your\venv\testpip\lib\site-packages (python 3.5) 
    

注意不要做同樣的setuptools因爲它有一些其他的依賴,這使得問題更加複雜。

快樂黑客:)

相關問題