# Python
### Python3 - Set up virtual enviroment
Check this article https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/
NOTE: There are two tools for setting up virtual environments, virtualenv and venv, that we can use almost interchangeably. virtualenv supports older Python versions and needs to be installed using the pip command. In contrast, venv is only used with Python 3.3 or higher and is included in the Python standard library, requiring no installation.
Create a dir for the project
```bash
mkdir myproject
```
Create the virtual env
```bash=
python3 -m venv myproject/venv
```
Activate the virtual env
```bash
source myproject/venv/bin/activate
┌──(venv)─(kali㉿kali)-[~/Downloads]
└─$
┌──(venv)─(kali㉿kali)-[~/Downloads]
└─$ which python
/home/kali/Downloads/myproject/venv/bin/python
┌──(venv)─(kali㉿kali)-[~/Downloads]
└─$ python --version
Python 3.10.7
```
Installing Packages in a Python Virtual Environment
```bash
┌──(venv)─(kali㉿kali)-[~/Downloads]
└─$ pip list
Package Version
---------- -------
pip 22.2
setuptools 59.6.0
┌──(venv)─(kali㉿kali)-[~/Downloads]
└─$ pip install --upgrade pip
```
Install specific requirements for your project
```bash
┌──(venv)─(kali㉿kali)-[~/Downloads]
└─$ pip install -r requirements.txt
```
### Install python2 and set up a virtual environment
Check this article https://realpython.com/intro-to-pyenv/#specifying-your-python-version
#### Install python2
Check this article https://www.kali.org/docs/general-use/using-eol-python-versions/
Install dependencies
```bash
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git
```
The following configuration apply if you are using ZSH shell
Check if you are using ZSH
```bash=
ps -p $$
```
or
```bash
echo $SHELL
```
Install pyenv.run https://github.com/pyenv/pyenv-installer
```bash
curl https://pyenv.run | bash
```
If we are using ZSH then we will now add the proper lines to our .zshrc.
Update the shell to ensure autocomplete for pyenv is functional
```bash=
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init --path)"\nfi' >> ~/.zshrc
```
Lets continue with the setup. Reboot shell
```bash
exec $SHELL
```
Check pyenv
```bash
pyenv
```
We can now install Python 2 and set it as our default Python version:
```bash
pyenv install 2.7.18
pyenv global 2.7.18
```
Check python2 version
```
pyenv versions
python
```
We can now install dependencies as needed for whatever tools we are using.
When we want to swap back to Python 3, we just need to install python 3 and set the global to be system. One thing to keep in mind is to stick with installing the dependencies via pip
```bash=
pyenv install --list | grep 3.10
pyenv install 3.10.7
┌──(kali㉿kali)-[~/Downloads]
└─$ pyenv versions
system
* 2.7.18 (set by /home/kali/.pyenv/version)
3.10.7
pyenv global 3.10.7
┌──(kali㉿kali)-[~/Downloads]
└─$ pyenv versions
system
2.7.18
* 3.10.7 (set by /home/kali/.pyenv/version)
```
#### Set up the virtual environment
Make a note of the full file path to the custom version of Python you just installed
```bash
which python
/home/kali/.pyenv/shims/python
```
Install virtual-event
```bash
sudo apt install python3-virtualenv
```
Create the virtual env using python2
virtualenv -p \<path to python2\> \<virtual dir\>
```bash=
virtualenv -p /home/kali/.pyenv/shims/python venv
```
Activate the virtual environment
```bash
source venv/bin/activate
```
Now you will work with python2
```
──(venv)─(kali㉿kali)-[~]
└─$ python -V
Python 2.7.18
┌──(venv)─(kali㉿kali)-[~]
└─$ pip --version
pip 20.3.4 from /home/kali/venv/lib/python2.7/site-packages/pip (python 2.7)
```
## Pyenv MacOS
https://github.com/pyenv/pyenv#how-it-works
### Visual Studio Code - Set up a virtual environment
Opening the Command Palette (Shift+⌘+P)
Select Python Interpreter
Then open Terminal (Control+Shift+P)
```bash=
python3 -m venv .venv
```
Accept the prompt to allow you to select it for the workspace.