Skip to main content

Detect if your script is running inside a virtual environment

Project description

isVirtual is a very simple tool to detect if the current script is within a virtual environment.

Disclaimer

The goal of this project was to play around with the process to publish to Pypi. The code of this module is coming from this stackoverflow thread.

If you find use cases in which it doesn't work please open an issue. I intend to maintain this small package even if it can be seen as "useless".

Install

pip install isvirtual

Usage

This lib can be used within a python script or as a command line.

Python

Simple check:

from isvirtual import is_virtual_env

if __name__ == "__main__":
    if is_virtual_env() is True:
        print("You are within a virtual environment")
    else:
        print("You are not in a virtual env")

You can also check if you are specifically in a venv or virtualenv environment:

from isvirtual import is_venv, is_virtualenv

if __name__ == "__main__":
    if is_venv() is True:
        print("You are in a venv")
    elif is_virtualenv() is True:
        print("You are in a virtualenv")
    else:
        print("You are not in a virtual env")

You can also get the info from the env coming from pyvenv.cfg. The sys.prefix data is added to the original config file:

from isvirtual import is_virtual_env, pyvenv_cfg

if __name__ == "__main__":
    if is_virtual_env() is True:
        data = pyvenv_cfg()
        print(data["home"])

CLI

$ isvirtual
Yes

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

isvirtual-1.1.0.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

isvirtual-1.1.0-py3-none-any.whl (4.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page