Skip to content

Latest commit

Β 

History

History
307 lines (194 loc) Β· 8.1 KB

z__ENV_VIRTUALENV .md

File metadata and controls

307 lines (194 loc) Β· 8.1 KB

🟑 VIRTUALENV


  • Before installing anything related to the VIRTUALENV, check if you already have this: z__ENV_PATH_issue, if you have it, you can continue, but if not, just go the link and follow the steps.


🟠 VirtualEnv

  • virtualenv is a third-party tool that allows more flexibility in specifying Python versions and has different default behaviors compared to python -m venv.

  • The bin/ directory within env/ includes βœ‹ activation scripts (activate, activate.bat for Windows) and other necessary files for managing the virtual environment.



🟣 Question: what are the differences between python -m venv & virtualenv?


chatgpt: 🟧 Key Differences:

python -m venv is part of Python's standard library (venv module), while virtualenv is a third-party tool.

Activation Scripts: virtualenv typically provides more comprehensive activation scripts (activate, activate.bat) within the bin/ directory, making it easier to activate the virtual environment.

Python Version: virtualenv βœ‹ allows specifying the Python interpreter version explicitly (-p python3), whereas python -m venv uses the default Python interpreter in your system's PATH.


🟒 Conclusion:

Preferred Tool: If you need more control over Python versions or desire a more consistent activation experience across different platforms, virtualenv with explicit Python version (-p) is often preferred.

Standard Library vs. Third-Party: Both python -m venv and virtualenv are widely used for creating virtual environments. Your choice may depend on specific project requirements or personal preference.



🟑 installation & common issues


Works on ubuntu

  • vistualenv works on the ubuntu terminal.

  • ubuntu terminal: when i type the ~$ virtualenv --version command, i get this:

// output
virtualenv 20.26.3 from /home/mycomputer/.local/lib/python3.8/site-packages/virtualenv/__init__.py

Don't work on VS

  • BUT when i try the same command ~$ virtualenv --version in my visual studio , on that specific project LESSON_17_Virtual_Environment_and_pip, i get this:
virtualenv: command not found


🟣 Question: why this command vistualenv, work on the global but not on a local project in VS ?

βœ… chatgpt:

It seems like there might be an issue with the environment variables or PATH configuration in your Visual Studio terminal, which is causing the virtualenv command not to be found.


Here’s how you can troubleshoot and potentially resolve this issue:

  • First check if you have any output after you type this echo $PATH in your ubuntu terminal:
// type this:
echo $PATH
// OUTPUT
/usr/local/bin:/usr/bin:/bin:/home/mycomputer/.local/bin:/snap/bin

βœ… chatgpt:

  • Based on the output of echo $PATH, it appears that /home/dci-st119/.local/bin is already included in your PATH variable. This is where virtualenv is installed according to your previous messages.

πŸ”΄ But if you dont get the above output

FOLLOW THIS STEPS

after the above steps


  1. πŸ”Έ Verify Visual Studio Terminal Shell
  • Check which shell Visual Studio's integrated terminal is using and ensure it matches your expectations for running virtualenv commands.
// type this in your VS terminal
echo $SHELL
//
// βœ‹ Output Interpretation:
// This command will display the shell currently active in your terminal session.
output:
/bin/bash, /usr/bin/zsh, /usr/bin/powershell, etc.

  1. πŸ”Έ Restart Visual Studio
  • Restart Visual Studio to clear any temporary configuration issues that might be affecting virtualenv recognition.

  1. πŸ”Έ Directly Invoke virtualenv
  • Attempt to directly execute virtualenv using its absolute path to confirm it's executable.

Command: /home/mycomputer/.local/bin/virtualenv --version

// should look like this:
LESSON_17_Virtual_Environment_and_pip$ /home/mycomputer/.local/bin/virtualenv --version
//
// Output Interpretation:
// If virtualenv is found and executable, it will output its version number.

virtualenv 20.26.3 from /home/mycomputer/.local/lib/python3.8/site-packages/virtualenv/__init__.py

  1. πŸ”Έ Check Visual Studio Terminal Settings
  • (OPTIONAL) Review Visual Studio's terminal settings to ensure it has the correct permissions and environment settings to execute commands from /home/dci-st119/.local/bin.

  1. πŸ”Έ Update Visual Studio Terminal PATH

πŸ”΄If Visual Studio's terminal does not recognize virtualenv, manually update its PATH setting.

type this on VS terminal of your project
  • βœ‹ Command (temporary update):
export PATH=$PATH:/home/mycomputer/.local/bin

  1. πŸ”Έ option
Command (permanent update in .bashrc for Bash):
//1
echo 'export PATH=$PATH:/home/mycomputer/.local/bin' >> ~/.bashrc

//2
source ~/.bashrc
//
//This command uses source to execute (or "source") the commands from the .bashrc file in the current shell session. By doing so, it updates the PATH environment variable immediately in your current terminal session without the need to start a new shell.

//
// So, you would typically run these two commands consecutively in your terminal. The first command updates .bashrc to include /home/mycomputer/.local/bin in your PATH, and the second command refreshes your current shell session to apply this change.

  1. πŸ”Έ now check it:

TYPE: virtualenv --version

OUTPUT
virtualenv 20.26.3 from /home/mycomputer/.local/lib/python3.8/site-packages/virtualenv/__init__.py


🌈 Continue with the activation


🍭 creating the env

  • creating the env with virtualEnv

  • choose the project where you are going to install the env folder, in my case i will choose /LESSON_16_OOP , once you have cd into that project, type the following:

virtualenv -p python3 env

// -  -p python3: This specifies that you want to use Python 3 as the interpreter for creating the virtual environment.
//
// - If you omit the -p option, virtualenv will use the default Python interpreter that is available on your system when creating the virtual environment.
//

output

created virtual environment CPython3.8.10.final.0-64 in 307ms
  creator CPython3Posix(dest=/home/mycomputer/Documents/ALL_SITE_3D_STUFF/3D-UNITY-BLENDER-REACTVR-ALL/0_PYTHON-all/PYTHON-PRIVAT/python-intro-2024-privat/LESSON_16_OOP/env, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/mycomputer/.local/share/virtualenv)
    added seed packages: pip==24.1, setuptools==70.1.0, wheel==0.43.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

🍭 Once the ENV has been created, you will see the following:

  • πŸ“Œ env (folder)
    • bin (folder, it includes the activation files and other stuff)
    • lib (folder)
    • gitignore
    • pyvenv.cfg

🍭 Activate

  • type the following command (you have to be on your project root)
source env/bin/activate
//
// like so:
LESSON_16_OOP$ source env/bin/activate

output

// the env is there, it means it works
/LESSON_16_OOP$ source env/bin/activate
(env) dci-st119@mycomputer:



πŸ”΄ Now tht i am inside the virtual environment like you can see in the below code, you can check the packages, type this command:

pip list

output

Package    Version
---------- -------
pip        24.1
setuptools 70.1.0
wheel      0.43.0

like so:

/LESSON_16_OOP$ pip list
Package    Version
---------- -------
pip        24.1
setuptools 70.1.0
wheel      0.43.0
(env) dci-st119@mycomputer:~/



🟠 Installing other packets

  • Now that I have the env setup, i can install stuff but ONLY in this env (environment)
pip install flask

What is flask?