isvirtual


Nameisvirtual JSON
Version 1.5.0 PyPI version JSON
download
home_pageNone
SummaryTool 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.
upload_time2024-05-31 12:25:05
maintainerNone
docs_urlNone
authorAlex Mili
requires_python>=3.3
licenseMIT License Copyright (c) 2016 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords env environment hatch pdm pipenv poetry uv venv virtual virtualenv
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            **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.

# Install

```bash
pip install isvirtual
```

# Usage

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

## Python
Simple check:
```python
from isvirtual import is_virtual

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:
```python
from isvirtual import is_venv, is_virtualenv, is_conda

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`:
```python
from isvirtual import get_config

if __name__ == "__main__":
    data = get_config()
    print(data["home"])
```
Result:
```console
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.

You can also check if the terminal from which the script has been launched is in a virtual env:
```python
if running_from_activated_env() is True:
    print("The script is running from a terminal with an activated virtual env")
```

Note 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.

[Source](https://stackoverflow.com/a/1883251)

You can find if a given directory is attached to a virtual env:
```python
from isvirtual import check_dir

if __name__ == "__main__":
    if check_dir("/some/dir/path") is True:
        print("Virtual environment found")
    else:
        print("404 Not Found")
```

You can recursively scan a directory to get all virtual environments in it:
```python
from isvirtual import scan_dir

if __name__ == "__main__":
    scanned = scan_dir("/some/dir/path")
    if len(scanned) > 0:
        print(f"Found {len(scanned)} virtual env(s)")
    else:
        print("No virtual env found")
```

## CLI
```console
$ isvirtual --help
Usage: isvirtual [OPTIONS] COMMAND [ARGS]...

╭─ Options ──────────────────────────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                                            │
╰────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ─────────────────────────────────────────────────────────────────────────────╮
│ check   Check if you are currently in a virtual env                                    │
│ info    If the given directory is linked to a virtual env, show its info               │
│ scan    Scan the given directory recursively to find all virtual environment in it     │
╰────────────────────────────────────────────────────────────────────────────────────────╯
```

Check if a virtual env is currently activated (support conda):
```console
$ isvirtual check
Yes
```

Show info of current directory's virtual env info:
```console
$ isviritual info .
home=/usr/local/opt/python@3.10/bin
include-system-site-packages=false
version=3.10.14
path=/Users/MyUserName/MyDir/.venv
source=venv
```
Also display with the `source` key if the virtual env has been created through `venv`, `virtualenv`, `poetry` and `pipenv`.

# License

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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "isvirtual",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.3",
    "maintainer_email": null,
    "keywords": "env, environment, hatch, pdm, pipenv, poetry, uv, venv, virtual, virtualenv",
    "author": "Alex Mili",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/78/56/4c3e1417d99384de123454917a82e02d35f5441405f801de2b71eec4f567/isvirtual-1.5.0.tar.gz",
    "platform": null,
    "description": "**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) > 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\u256d\u2500 Options \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n\u2502 --help          Show this message and exit.                                            \u2502\n\u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f\n\u256d\u2500 Commands \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256e\n\u2502 check   Check if you are currently in a virtual env                                    \u2502\n\u2502 info    If the given directory is linked to a virtual env, show its info               \u2502\n\u2502 scan    Scan the given directory recursively to find all virtual environment in it     \u2502\n\u2570\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u256f\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",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2016  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "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.",
    "version": "1.5.0",
    "project_urls": {
        "Documentation": "https://github.com/AlexMili/isVirtual",
        "Homepage": "https://github.com/AlexMili/isVirtual",
        "Issues": "https://github.com/AlexMili/isVirtual/issues",
        "Repository": "https://github.com/AlexMili/isVirtual"
    },
    "split_keywords": [
        "env",
        " environment",
        " hatch",
        " pdm",
        " pipenv",
        " poetry",
        " uv",
        " venv",
        " virtual",
        " virtualenv"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e81a2e141fa526154a297501dbc3c4790e5150cae9e9ea3a08bde67b35ec741e",
                "md5": "9f8f0ea805d8a0d0979f611ae4283d38",
                "sha256": "b4fdec5ab12d1b0049f6320e97142669ab164f9d883ae9b447bb5c051af506be"
            },
            "downloads": -1,
            "filename": "isvirtual-1.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9f8f0ea805d8a0d0979f611ae4283d38",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.3",
            "size": 7885,
            "upload_time": "2024-05-31T12:25:04",
            "upload_time_iso_8601": "2024-05-31T12:25:04.269810Z",
            "url": "https://files.pythonhosted.org/packages/e8/1a/2e141fa526154a297501dbc3c4790e5150cae9e9ea3a08bde67b35ec741e/isvirtual-1.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "78564c3e1417d99384de123454917a82e02d35f5441405f801de2b71eec4f567",
                "md5": "9f6f3111c2ccc311050ac98850babdd8",
                "sha256": "4594502238043717c4b5f65c6a0d61b6ebfc9b0cea4add8368d4871d6c1f6a8f"
            },
            "downloads": -1,
            "filename": "isvirtual-1.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "9f6f3111c2ccc311050ac98850babdd8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.3",
            "size": 6636,
            "upload_time": "2024-05-31T12:25:05",
            "upload_time_iso_8601": "2024-05-31T12:25:05.316356Z",
            "url": "https://files.pythonhosted.org/packages/78/56/4c3e1417d99384de123454917a82e02d35f5441405f801de2b71eec4f567/isvirtual-1.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-31 12:25:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AlexMili",
    "github_project": "isVirtual",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "isvirtual"
}
        
Elapsed time: 0.25833s