I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
-
2A virtual environment has to link to a preexisting Python. If you don't have 3.6 installed, you need to install it first, *before* creating the virtual environment. – chepner Dec 23 '20 at 19:43
-
Thank you for clarifying this. I am most likely going to have to use some other solution if I need specific python versions to be installed on target platform as I do not have access rights to change anything on that computer. – Russ1337 Dec 24 '20 at 03:22
2 Answers
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using. If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller. https://realpython.com/pyinstaller-python/

- 1,144
- 12
- 17
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate
command.
To deactivate environment simply run: deactivate

- 91
- 2
- 10
-
This is similar to how I am doing it with pipenv. I have also used a command script to get to run on both windows and linux. However, my new requirement expects me to handle it independent of the python version on other platforms. This might mean that the solution I seek cannot be handled by virtual environments alone. Thanks. – Russ1337 Dec 24 '20 at 03:26
-
@Russ1337 Well if it must be independent from the platform, in that case only Docker comes in my mind. To enclose everything inside your custom made Docker image. But I don't know if there are limitation for a such solution and you could potentially use it. – dubaksk Dec 24 '20 at 20:39