简体   繁体   English

如何在python控制台中使用pip安装本地包

[英]How to install a local package using pip in python console

I am not sure if this is because I work in my company and there is a proxy, I cannot set the environment variable. 我不确定这是因为我在我的公司工作并且有代理,我无法设置环境变量。 So, I cannot use pip install in CMD. 所以,我不能在CMD中使用pip install。 I try to download the package, to my local machine, and install it using the method from Installing python module within code . 我尝试将软件包下载到我的本地机器,并使用安装python模块中的方法在代码中安装它。 But I failed. 但我失败了。 Here is my code: 这是我的代码:

import pip
pip.main(['install','h://feng.officeworks/mixed/myPython/numpy-1.12.0'])

This does not work for me, I have the following information: 这对我不起作用,我有以下信息:

Invalid requirement: 'h://feng.officeworks/mixed/myPython/numpy'
Traceback (most recent call last):
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\packaging\requirements.py", line 92, in __init__
    req = REQUIREMENT.parseString(requirement_string)
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\pyparsing.py", line 1617, in parseString
    raise exc
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\pyparsing.py", line 1607, in parseString
    loc, tokens = self._parse( instring, 0 )
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\pyparsing.py", line 1379, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\pyparsing.py", line 3376, in parseImpl
    loc, exprtokens = e._parse( instring, loc, doActions )
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\pyparsing.py", line 1383, in _parseNoCache
    loc,tokens = self.parseImpl( instring, preloc, doActions )
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\pyparsing.py", line 3164, in parseImpl
    raise ParseException(instring, loc, self.errmsg, self)
pip._vendor.pyparsing.ParseException: Expected stringEnd (at char 1), (line:1, col:2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\req\req_install.py", line 82, in __init__
    req = Requirement(req)
  File "C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pip\_vendor\packaging\requirements.py", line 96, in __init__
requirement_string[e.loc:e.loc + 8]))
pip._vendor.packaging.requirements.InvalidRequirement: Invalid requirement, parse error at "'://feng.'"
1

I am using windows 10, Python 3.6.0, Pycharm 2016.3.2. 我使用的是Windows 10,Python 3.6.0,Pycharm 2016.3.2。 pip is already installed with Python pip已经安装了Python

Also, because I do not have access to environment variable, I cannot set the path to Python. 另外,因为我无法访问环境变量,所以我无法设置Python的路径。 So I cannot use the orders like python or pip in a window terminal. 所以我不能在窗口终端中使用像python或pip这样的命令。 So I need to use a function in python console. 所以我需要在python控制台中使用一个函数。

Thanks a lot for the answers. 非常感谢您的回答。 I also tried to go to C:\\Users\\fchen4\\AppData\\Local\\Programs\\Python\\Python36-32\\Scripts, then use 我也试过去C:\\ Users \\ fchen4 \\ AppData \\ Local \\ Programs \\ Python \\ Python36-32 \\ Scripts,然后使用

pip install h://mypath/numpy. 

I already unpack numpy here. 我已经在这里解开numpy了。 It does not work. 这是行不通的。 The information shown in PowerShell is like: PowerShell中显示的信息如下:

Obtaining file:///H:/feng.officeworks/mixed/myPython/numpy
Installing collected packages: numpy
  Running setup.py develop for numpy
    Complete output from command c:\users\fchen4\appdata\local\programs\python\python36-32\python.exe -c "import setuptools, tokenize;__file__='H:\\feng.officeworks\\mixed\\m
yPython\\numpy\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" develop --no-deps:
    blas_opt_info:
    blas_mkl_info:
      libraries mkl_rt not found in ['c:\\users\\fchen4\\appdata\\local\\programs\\python\\python36-32\\lib', 'C:\\', 'c:\\users\\fchen4\\appdata\\local\\programs\\python\\py
thon36-32\\libs']
      NOT AVAILABLE

    .... (There are too much here so I ignore it.)
    ....
Command "c:\users\fchen4\appdata\local\programs\python\python36-32\python.exe -c "import setuptools, tokenize;__file__='H:\\feng.officeworks\\mixed\\myPython\\numpy\\setup.py
';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" develop --no-deps" failed with error code 1
 in H:\feng.officeworks\mixed\myPython\numpy\

Anyone could please tell me how to install a local package using pip or anything else in python console? 有人可以告诉我如何在python控制台中使用pip或其他任何东西安装本地软件包吗?


Install a particular source archive file: 安装特定的源存档文件:

pip install ./downloads/SomePackage-1.0.4.tar.gz

Also you can download the module source distribution and install it, unpack the archive into a similarly-named directory: foo-1.0 . 您也可以下载模块源代码并安装它,将存档解压缩到一个名为foo-1.0目录中。 Additionally, the distribution will contain a setup script setup.py ,and then run this command from a terminal: 此外,该发行版将包含一个安装脚本setup.py ,然后从终端运行此命令:

python setup.py install

If you want to install package within code,try this: 如果要在代码中安装包,请尝试以下操作:

import os
os.system("pip2.7 install foo")

Or 要么

import subprocess
subprocess.call(['pip', 'install', foo])

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM