Skip to content

Commit

Permalink
Merge pull request #6 from outbit/feature/select_python_version
Browse files Browse the repository at this point in the history
Feature/select python version
  • Loading branch information
thedavidwhiteside authored Apr 10, 2024
2 parents bcf555c + 5b5f8fb commit 1183acf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
url='https://github.com/outbit/tempyenv',
license='MIT',
install_requires=[
'setuptools'],
'setuptools',
'venv'],
include_package_data=True,
package_dir={
'': 'src'},
Expand Down
7 changes: 7 additions & 0 deletions src/tempyenv/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def main():
from tempyenv import cli
cli.main()


if __name__ == "__main__":
main()
19 changes: 16 additions & 3 deletions src/tempyenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
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()
self.venv_path = os.path.join(self.temp_dir.name, 'venv')

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}")
Expand All @@ -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()

4 changes: 4 additions & 0 deletions tests/units/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 1183acf

Please sign in to comment.