Create python venvs in restricted environments

Prasanna Wijesiriwardana
3 min readNov 28, 2022

--

Creating virtual environments and utilize them for development is a good practice as it creates an isolated environment and will not affect any other applications. There are quite a few tools such as venv, conda, pipenv which can be used to create and manage virtual environments very easily.

Duplicating a virtual environment also really easy task. All you need is the requirements.txt from the source environment and one simple pip install command. After few minutes it should be replicated and ready to use.

However sometimes this can be challenging in some production environments as they are highly controlled due to compliance and security measures. Therefore, getting access to package repositories which is required to setup a virtual environment may be blocked or restricted as per the security policies. Following is a workaround to export python virtual environment to restricted environment.

And I like to reiterate, best practice would be to create the virtual environment using pip install or a similar method.

Generate the virtual environment using the wheel files

Prerequisites: Originating PC/Server and the destination pc/server both must have the same python versions.

Step 1 — Generate package list from the source environment

Generate file contains the list of packages in the current environment.

pip freeze -l > packages.txt

Step 2 — Generate wheel files from the source environment.

Generate wheel files for all the installed packages in the source environment using the packages.txt

pip wheel -w wheels_folder -r packages.txt

Step 3- Create the new virtual environment

Copy the folder contains the generated wheels files to the destination. Once done, create new virtual environment using the venv command and activate it.

Create requirements.txt file with all the wheel files.

then run pip install -r requirements.txt to get them all install at once

pip install -r requirements.txt

Known issues

Failed building wheel for X while generating wheel files

In order to generate wheel files successfully wheel package is required. This is being used to regenerate the wheel files when the package is required to rebuild the wheel files due to some error.

pip install wheel

Installing the wheel package in the source environment before generating the wheel files for the installed packages will solve this error.

Unable to import sqllit3 using Anaconda Python

Copy the sqllit3.dll from the originating server python folder to the Anaconda distributions DLL folder

Ex:

C:\Users\<USER>\Anaconda3\DLLs

or

C:\ProgramData\Anaconda3\DLLs

Conclusion

There are other ways to export venvs to different locations. However in my opinion preferred way is to generate the virtual environment using the wheel files, as it requires minimum manual intervention. However, note that, destination and source python versions as well as operating systems must match this to work as expected.

Finally as mentioned earlier, this is a workaround to export virtual environments when destination server cannot access required resources to generate a virtual environment and should not be used as general practice.

Thanks for reading!

References

--

--

Prasanna Wijesiriwardana
Prasanna Wijesiriwardana

Written by Prasanna Wijesiriwardana

Data Engineer | Loves Python, Data and Cloud | Linkedin - www.linkedin.com/in/prasas

No responses yet