3

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. I try to download the package, to my local machine, and install it using the method from Installing python module within code . 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. pip is already installed with Python

Also, because I do not have access to environment variable, I cannot set the path to Python. So I cannot use the orders like python or pip in a window terminal. So I need to use a function in python console.

Thanks a lot for the answers. I also tried to go to C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\Scripts, then use

pip install h://mypath/numpy. 

I already unpack numpy here. It does not work. The information shown in PowerShell is like:

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?


Community
  • 1
  • 1
Feng Chen
  • 2,139
  • 4
  • 33
  • 62
  • "But I failed" -> Please copy-paste the errors that you have, else it is hard to understand your failure. Also, for reference, the version of Python and of pip can be useful. – Pierre de Buyl Feb 13 '17 at 10:42

1 Answers1

3

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. Additionally, the distribution will contain a setup script setup.py,and then run this command from a terminal:

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.

McGrady
  • 10,869
  • 13
  • 47
  • 69
  • Thanks a lot for your answer. But still does not work. I tried like: subprocess.call(['pip', 'install', 'numpy']). It looks like still trying to getting the library from Internet. The information is like this: Collecting numpy Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(, 'Connection to pypi.python.org timed out. (connect timeout=15)')': /simple/numpy/ – Feng Chen Feb 14 '17 at 00:01
  • Also, I cannot use an order like pip install ./downloads/SomePackage-1.0.4.tar.gz because I cannot use python command in a window terminal – Feng Chen Feb 14 '17 at 00:03
  • @FengChen `Connection to pypi.python.org timed out` changing [pypi mirrors](https://www.pypi-mirrors.org/) maybe helps.Also you can run `cd C:\Users\fchen4\AppData\Local\Programs\Python\Python36-32\Scripts ` and then use `pip` to install. – McGrady Feb 14 '17 at 01:35
  • Thanks! Still got sth wrong. under ...\Scripts, I use pip install h://mypath/numpy. I got some error information. I already edit my question according to this. Could you please take a look at it? Thanks a lot – Feng Chen Feb 14 '17 at 02:27
  • @FengChen Check out [this answer](http://stackoverflow.com/questions/28413824/installing-numpy-on-windows) – McGrady Feb 14 '17 at 02:29
  • Thanks a lot for your patience. I think my problem is my company block almost everything. I need to ask them for the authorization. – Feng Chen Feb 14 '17 at 22:49