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() is True:
print("You are within a virtual environment which can either be venv, virtualenv or conda.")
else:
print("You are not in a virtual env")
You can also check if you are specifically in a venv
, virtualenv
or conda
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")
elif is_conda() is True:
print("You are in a conda env")
else:
print("You are not in a any type of virtual env")
You can also get the info from the env coming from pyvenv.cfg
or load equivalent data from conda
config. The sys.prefix
data is added to the original config file under the key prefix
:
from isvirtual import is_virtual_env, get_config
if __name__ == "__main__":
data = get_config()
print(data["home"])
Result:
home = /path/to/venv/python/bin
include-system-site-packages = false
version = 3.10.14
prefix = /path/to/venv/dir
prompt = nameOfYourProject
Note that virtual environment created with virtualenv
have more keys and the key prompt
is not present by default in venv
created environments.
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
Built Distribution
Hashes for isvirtual-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e6fc91614723f50681dd75f23b52d75a1eb0239ff30e7a1b569468695e62db2 |
|
MD5 | 0afd24bafb7c399c79f0aa172e2675e0 |
|
BLAKE2b-256 | 1c2be62426e21549b15c337fedf3a89c0547998d6af35dae8295e3fb2ff66182 |