Skip to content

Instantly share code, notes, and snippets.

@MiCurry
Last active February 25, 2019 21:28
Show Gist options
  • Save MiCurry/726accbe6e86e90b2620b9a4bcddc8d8 to your computer and use it in GitHub Desktop.
Save MiCurry/726accbe6e86e90b2620b9a4bcddc8d8 to your computer and use it in GitHub Desktop.
Python Venv Examples
# In order for me to run, numpy has to be avaiable to my python installation!
import numpy
my_array = [10, 3, 4, 5, 100]
my_array = numpy.array(my_arry)
print(type(my_array))
print(my_array)
# Python Venv Example
import sys
import os
import subprocess
import shutil
import venv
"""
Venv - Python virtual environment module.
Pip - Package installer for python - Pip now comes pre-installed for Python
version >= 2.7.9 and/or Python 3 >= 3.4. But for previous version if
they're to be supported, it'll need to be downloaded manually.
"""
# Here is the path we will create our environment ('./env')
path = os.path.join('./', 'env')
# Remove the directory if it already exists
if os.path.exists(path):
shutil.rmtree(path)
# Instantiate the environment builder - `with_pip=True` so that python will
# automatically install pip into our environment directory.
builder = venv.EnvBuilder(with_pip=True)
builder.create(path)
# Check to see if the directory exists
if not os.path.exists(path):
print("The virtual environment directory does not exist! :(")
sys.exit(-1)
else:
print("The virtual environment was created successfully! :)\n")
print("System wide python installation location: ")
subprocess.run(["which","python3"])
# Now set the system to use the python3 and pip that is inside our
# virtual environment:
full_env_path = os.path.join(os.getcwd(), 'env')
# Append the virtual environment to the front of $PATH, so that the os will
# look there first for our env's pip and python installations, (and not the
# system wide ones)
os.environ['PATH'] = full_env_path+'/bin/'+':'+os.environ['PATH']
print("\nLocal environment python3 installation location: ")
subprocess.run(["which","python3"])
# As well, we now have a local pip installation which we can now use to install
# a python packages for our environment!
print("\nLocal environment pip3 installation location: ")
subprocess.run(["which","pip3"])
# Now lets install some local packages!
# Lets install Numpy!
# First, lets check to see if Numpy has been installed in our virtual
# environment.
# running `pip freeze` will produce a list of what is installed in a convenient
# format.
print("Available packages: ")
subprocess.run(['pip3', 'freeze']) # This should output nothing at this time
print("\nInstalling Numpy: ")
subprocess.run(['pip3', 'install', 'numpy'])
print("\n Running `pip3 freeze` again...(Numpy should now be displayed")
subprocess.run(['pip3', 'freeze']) # This should output nothing at this time

Python3 Venv Command Line Example

First, check to see where our current python installation is located:

> which python # And
> which python3

As well, see if there is a pip installation on the global system (some systems will not have a system wide installation i.e. chyenne)

> which python3
> which pip3

Create the vitural enviornment on the command line:

> python3 -m venv cl_env 

Check to make sure the enviornment we created exists. You should see that there is now a directory called cl_env in our current dirrectory.

> ls

Source the enviornment so that our shell gains access to it and its installed modules:

> source cl_env/bin/activate  # For Bash
> source cl_env/bin/activate.csh  # For tcsh or csh
[cl_env] > # Our prompt should now change to refelct that we have the enviornment
[cl_env] > # as these new lines have.

Now check to see where our Python and Pip installations are located:

[cl_env] > which python3   # The output should look something like the following:
.../cl_env/bin/python3
[cl_env] > which pip3   # Likewise the output should look something like:
.../cl_env/bin/pip3

Now lets install numpy for our python3 in our vitural enviornment:

[cl_env] > pip3 install numpy
Loads of pip output here, which should end with: 'Successfully installed numpy-' followed by a version number
[cl_env] > # Check to see what packages we have instalelled (Should be just numpy)
[cl_env] > pip3 freeze
numpy==1.16.1

Now run numpy_test.py to show that our enviornment has numpy installed:

python numpy_test.py

Return to our orginal enviornment by typeing decativate

[cl_env] > deactivate
> # Promp changes back to normal, and now our python installations should be where they were before.
> which python3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment