4

I am filling in for someone while they are on vacation and I am new to python. I've been asked to install several packages in a virtual environment. The big catch is the server cannot be on a public net so I will be downloading the software on a different server and copying it to the server where the researcher will be working.

I found another thread "Install Virtualenv without internet connectivity" at Install Virtualenv without internet connectivity, but that doesn't fit the situation I am in - it looks like they can start from a server on the net to complete their installs and want to share that virtualenv to other systems in a lab environment that may not have Internet connectivity.

Another thread "python: How to create virtualenv without internet connection" at python: How to create virtualenv without internet connection, is similar but it looks like they already have virtualenv installed. I don't find virtualenv installed here.

This Windows Server 2016 system is locked down where I cannot copy and paste the commands I ran to provide the information below, so forgive any typos in the hastily written message. I found the python version installed by:

python --version
Python 3.6.2rc1

I have not been able to find an installer to download for virtualenv. Do I need to download the Python installer again, re-run it and select additional options?

Thank you for any help you are able to provide.

edited to add:

Based on feedback, I changed the command (in an administrative command window) to python -m venv [path] and I have been able to make some progress.

I have the ability to download gz, whl or other files and move them to this server to run them there, but this server cannot be put online to download the installers directly nor can it connect to a repo to download dependencies. I cannot set up the environment on a different machine that has connectivity and share it without violating the security requirements. Thank you for the link to the Python Package Offline Installation thread - I think I was so narrowly focused on the virtual environment that I missed that post.

Be_in_NY
  • 41
  • 1
  • 4
  • 1
    How would you like to retrieve the required packages if you don't have an internet connection and cannot access the machine's file system ? – gogaz Aug 31 '18 at 16:41
  • The command is `python -m venv`. Getting the packages you need is still a problem, but this is a common FAQ. – tripleee Aug 31 '18 at 16:42
  • Given that installing any Python package is likely to require downloading transitive dependencies, I'm not sure you can do better than preparing a setup environment on a machine with connectivity and sharing that. Can you provide more details on what exactly is different between your problem and the one you linked? Maybe outline the sequence of events - what you expect to be downloaded on a machine that is connected, and what commands should work on the one that is not? – millimoose Aug 31 '18 at 16:56

1 Answers1

6

You're using Python 3.6, which means venv is included and pip can be bootstrapped if necessary. Creating a venv does not need internet access:

python3 -m venv .venv --prompt=myvenv

Installing pip does not require internet access:

python3 -m ensurepip 

Installing a package from a local file does not require internet access:

python3 -m pip install --no-index --disable-pip-version-check ./mydist.tar.gz

Should you have more than one package to install (for example, if mydist has dependencies) you may specify a local directory as your --index-url instead of the index defaulting to PyPI.

A solution for people on older versions of Python is covered here.

wim
  • 338,267
  • 99
  • 616
  • 750
  • Thank you. I will give this a go. – Be_in_NY Aug 31 '18 at 20:11
  • This isn't correct. `python -m venv ...` will try and upgrade pip, when that fails it will just exit without creating the environment. When using `--without-pip`, you now have a venv without setuptools or pip and are now stuck. – Rebs Jan 20 '21 at 06:06
  • @Rebs No, it will not try and upgrade pip. It will use the [bundled pip wheel](https://github.com/python/cpython/tree/master/Lib/ensurepip/_bundled) which is included with Python. I create offline venvs all the time without internet access, it works just fine. – wim Jan 20 '21 at 06:42
  • It definitely fails for me with anaconda/py 3.6. Perhaps it's because I have internet access but it's firewall'ed like crazy? I had to do --without-pip and then python setup . setuptools and pip to get it working. – Rebs Jan 25 '21 at 10:57
  • @Rebs Firewall can not having anything to do with it, because there's nothing in the Python source code that will access internet in the first place. `venv` module [calls `ensurepip` in a subprocess](https://github.com/python/cpython/blob/eb9983c59b0683270328b5c40a115bb028209511/Lib/venv/__init__.py#L291-L298), and `ensurepip` uses the bundled wheel, [additionally passing `--no-index` which prevents accessing PyPI](https://github.com/python/cpython/blob/eb9983c59b0683270328b5c40a115bb028209511/Lib/ensurepip/__init__.py#L182). I don't use Anaconda, maybe they modify the stdlib scripts somehow. – wim Jan 25 '21 at 17:42