简体   繁体   中英

How exactly works Python venv folder? My project use the dependencies into this venv folder executing the project using a different Python version?

I am an absolute beginner with Python (I came from Java) and I have the following doubt about the venv folder in my project.

So basically I have this project structure:

在此处输入图片说明

As you can see it contains the venv folder. Reading on the Python official documentation: https://docs.python.org/3/library/venv.html

I can read:

The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.

So from what I can understand it means that, using venv , every project have its own venv folder containing:

  1. The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).

  2. The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)

This because my PyCharm IDE is set to use Python 3.7

If this reasoning is correct (I am asbolutly not sure) it means that when I run something like:

python3 packet_sniffer.py

the packet_sniffer.py script (contained in the previous project) will be run using the Python 3 bynary and the dependencies that are into my venv folder. Is it correct?

If my reasoning is correct I have the following doubt:

What happen when I run it using Python 2 by

python packet_sniffer.py

I suppose that it is not using anymore the Python 3 version embedded into the venv folder of my project. And what dependencies of scapy am I using? where does it come from?

As you can see running with python instead python3 I am using Python 2 version:

root@kali:~# python --version
Python 2.7.17
root@kali:~# python3 --version
Python 3.7.5

python virtual environments

The venv folder is a suggested standard for installing python virtual environments. To use the virtual environment you must first activate it.

Note : Activate your python virtual environment prior to installing any packages required by your application.

from your command line execute:

source venv/bin/activate
python packet_sniffer.py 

Note : You do not need to specify the specific version of python as it defaults to the version used to create the virtual environment.

PyCharm can be configured to utilize python virtual environments

https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html

If you create a virtual environment, and you then activate it, the python interpreter installed in this virtual environment will be run if you call any of these: python foo.py / python3 foo.py. There will be NO differences between calling python foo.py or python3 foo.py (if you installed python3 of course).

The lib folder will contain the associated dependencies that you install through pip in this virtual environment and when you use python interpreter through this virtual environment you will use the aforementioned dependencies

So from what I can understand it means that, using venv, every project have its own venv folder containing:

  1. The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).

  2. The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)

Using venv every project has its own virtual environment folder that you can name anything you want, containing yes the bin folder and the lib folder.

If you have the virtual environment activated then everything you say about executing python3 is correct. However, all other commands are still available and will run from wherever they are located assuming they can be found by your shell. So your python command is probably running the system-level installed version of python.

If you want to know exactly where that python interpreter is running from:

 python
 >>> import sys
 >>> print(sys.executable)
 C:\Python27\python.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM