diff --git a/README.md b/README.md index f3bef46..09c41de 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,17 @@ Virtual environment loading from /var/folders/4b/dnp21z017cg_rbgfdtzclqlm0000gn/ (tempyenv)(venv) $ echo "now you can pip install in your virtual environment" ``` +To specify a specific version of python +```bash +$ tempyenv -p python3.10 +``` + +or + +```bash +$ python3.10 -m tempyenv +``` + License ======= diff --git a/setup.py b/setup.py index 289c7f5..5754386 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,8 @@ url='https://github.com/outbit/tempyenv', license='MIT', install_requires=[ - 'setuptools'], + 'setuptools', + 'venv'], include_package_data=True, package_dir={ '': 'src'}, diff --git a/src/tempyenv/__main__.py b/src/tempyenv/__main__.py new file mode 100644 index 0000000..32efc61 --- /dev/null +++ b/src/tempyenv/__main__.py @@ -0,0 +1,7 @@ +def main(): + from tempyenv import cli + cli.main() + + +if __name__ == "__main__": + main() diff --git a/src/tempyenv/cli.py b/src/tempyenv/cli.py index f20c60f..ea39884 100644 --- a/src/tempyenv/cli.py +++ b/src/tempyenv/cli.py @@ -3,10 +3,15 @@ import os import sys + class TemporaryVenvCreator: - def __init__(self): + def __init__(self, python_exec=None): self.temp_dir = None self.venv_path = None + if python_exec is None: + python_exec=sys.executable + else: + self.python_exec = python_exec def create_temporary_directory(self): self.temp_dir = tempfile.TemporaryDirectory() @@ -14,7 +19,7 @@ def create_temporary_directory(self): def create_virtual_environment(self): try: - subprocess.run(['python3', '-m', 'venv', self.venv_path], check=True) + subprocess.run([self.python_exec, '-m', 'venv', self.venv_path], check=True) print(f"Virtual environment created at {self.venv_path}") except subprocess.CalledProcessError as e: print(f"Error creating virtual environment: {e}") @@ -33,13 +38,21 @@ def load_virtual_environment(self): except subprocess.CalledProcessError as e: print(f"Error loading virtual environment: {e}") + def main(): + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('-p', '--python', help='Specify the Python executable', dest='python_exec', default=sys.executable) + args = parser.parse_args() + python_exec = args.python_exec + print("(tempyenv) is setting up your virtual environment...hold tight") - venv_creator = TemporaryVenvCreator() + venv_creator = TemporaryVenvCreator(python_exec) venv_creator.create_temporary_directory() venv_creator.create_virtual_environment() venv_creator.load_virtual_environment() + if __name__ == "__main__": main() diff --git a/tests/units/test_cli.py b/tests/units/test_cli.py index 290f22b..3b7f65f 100644 --- a/tests/units/test_cli.py +++ b/tests/units/test_cli.py @@ -17,3 +17,7 @@ def test_create_temporary_directory(self): cli.create_temporary_directory() assert(cli.temp_dir != None) assert(cli.venv_path != None) + + def test_python_exec(self): + cli = TemporaryVenvCreator(python_exec="python_test") + assert(cli.python_exec == "python_test")