{"id":22697871,"url":"https://github.com/alexmili/isvirtual","last_synced_at":"2025-08-02T08:04:12.710Z","repository":{"id":296957326,"uuid":"804441234","full_name":"AlexMili/isVirtual","owner":"AlexMili","description":"Tool to detect if the current directory is linked to a virtual environment, get the config of this env and more. Works with venv, virtualenv, pipenv, poetry, hatch, pdm and uv.","archived":false,"fork":false,"pushed_at":"2024-05-31T12:24:30.000Z","size":28,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-03T09:07:46.226Z","etag":null,"topics":["environment","hatch","pdm","pipenv","poetry","uv","venv","venv-python","virtual","virtualenv"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexMili.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-05-22T15:38:33.000Z","updated_at":"2024-08-30T08:04:50.000Z","dependencies_parsed_at":"2025-06-03T16:11:30.116Z","dependency_job_id":"7eac8b58-9eb5-40fe-818b-b3023355686c","html_url":"https://github.com/AlexMili/isVirtual","commit_stats":null,"previous_names":["alexmili/isvirtual"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/AlexMili/isVirtual","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexMili%2FisVirtual","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexMili%2FisVirtual/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexMili%2FisVirtual/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexMili%2FisVirtual/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexMili","download_url":"https://codeload.github.com/AlexMili/isVirtual/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexMili%2FisVirtual/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268351015,"owners_count":24236329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment","hatch","pdm","pipenv","poetry","uv","venv","venv-python","virtual","virtualenv"],"created_at":"2024-12-10T05:15:59.740Z","updated_at":"2025-08-02T08:04:12.676Z","avatar_url":"https://github.com/AlexMili.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"**isVirtual** is a tool to detect if the current directory is linked to a virtual environment, get the config of this env and more. Work with venv, virtualenv, pipenv, poetry, hatch, pdm and uv.\n\n# Install\n\n```bash\npip install isvirtual\n```\n\n# Usage\n\nThis lib can be used within a python script or as a command line.\n\n## Python\nSimple check:\n```python\nfrom isvirtual import is_virtual\n\nif __name__ == \"__main__\":\n    if is_virtual() is True:\n        print(\"You are within a virtual environment which can either be venv, virtualenv or conda.\")\n    else:\n        print(\"You are not in a virtual env\")\n```\n\nYou can also check if you are specifically in a `venv`, `virtualenv` or `conda` environment:\n```python\nfrom isvirtual import is_venv, is_virtualenv, is_conda\n\nif __name__ == \"__main__\":\n    if is_venv() is True:\n        print(\"You are in a venv\")\n    elif is_virtualenv() is True:\n        print(\"You are in a virtualenv\")\n    elif is_conda() is True:\n        print(\"You are in a conda env\")\n    else:\n        print(\"You are not in a any type of virtual env\")\n```\n\nYou 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`:\n```python\nfrom isvirtual import get_config\n\nif __name__ == \"__main__\":\n    data = get_config()\n    print(data[\"home\"])\n```\nResult:\n```console\nhome = /path/to/venv/python/bin\ninclude-system-site-packages = false\nversion = 3.10.14\nprefix = /path/to/venv/dir\nprompt = nameOfYourProject\n```\n\nNote that virtual environment created with `virtualenv` have more keys and the key `prompt` is not present by default in `venv` created environments.\n\nYou can also check if the terminal from which the script has been launched is in a virtual env:\n```python\nif running_from_activated_env() is True:\n    print(\"The script is running from a terminal with an activated virtual env\")\n```\n\nNote that this function check the existence of `VIRTUAL_ENV`. It is set by the activate script, but a virtual env can be used without activation by directly running an executable from the virtual env's bin/ (or Scripts on Windows) directory, in which case `VIRTUAL_ENV` will not be set. Or a non-virtual env Python binary can be executed directly while a virtual env is activated in the shell, in which case `VIRTUAL_ENV` may be set in a Python process that is not actually running in that virtual env.\n\n[Source](https://stackoverflow.com/a/1883251)\n\nYou can find if a given directory is attached to a virtual env:\n```python\nfrom isvirtual import check_dir\n\nif __name__ == \"__main__\":\n    if check_dir(\"/some/dir/path\") is True:\n        print(\"Virtual environment found\")\n    else:\n        print(\"404 Not Found\")\n```\n\nYou can recursively scan a directory to get all virtual environments in it:\n```python\nfrom isvirtual import scan_dir\n\nif __name__ == \"__main__\":\n    scanned = scan_dir(\"/some/dir/path\")\n    if len(scanned) \u003e 0:\n        print(f\"Found {len(scanned)} virtual env(s)\")\n    else:\n        print(\"No virtual env found\")\n```\n\n## CLI\n```console\n$ isvirtual --help\nUsage: isvirtual [OPTIONS] COMMAND [ARGS]...\n\n╭─ Options ──────────────────────────────────────────────────────────────────────────────╮\n│ --help          Show this message and exit.                                            │\n╰────────────────────────────────────────────────────────────────────────────────────────╯\n╭─ Commands ─────────────────────────────────────────────────────────────────────────────╮\n│ check   Check if you are currently in a virtual env                                    │\n│ info    If the given directory is linked to a virtual env, show its info               │\n│ scan    Scan the given directory recursively to find all virtual environment in it     │\n╰────────────────────────────────────────────────────────────────────────────────────────╯\n```\n\nCheck if a virtual env is currently activated (support conda):\n```console\n$ isvirtual check\nYes\n```\n\nShow info of current directory's virtual env info:\n```console\n$ isviritual info .\nhome=/usr/local/opt/python@3.10/bin\ninclude-system-site-packages=false\nversion=3.10.14\npath=/Users/MyUserName/MyDir/.venv\nsource=venv\n```\nAlso display with the `source` key if the virtual env has been created through `venv`, `virtualenv`, `poetry` and `pipenv`.\n\n# License\n\nThis project is licensed under the terms of the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmili%2Fisvirtual","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexmili%2Fisvirtual","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmili%2Fisvirtual/lists"}