Python Forum
import statement in a virtual environment - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: import statement in a virtual environment (/thread-32760.html)



import statement in a virtual environment - leodavinci1990 - Mar-04-2021

If I create a directory and activate a virtual environment inside it and then use PIP to install some packages.
Is this the case that as long as the virtual environment is active, the import statement will go to the installed locations of packages inside the virtual environment instead of the default PATH? And can I import the ones inside where Python is currently installed whilst the virtual environment is active?


RE: import statement in a virtual environment - snippsat - Mar-04-2021

(Mar-04-2021, 12:23 AM)leodavinci1990 Wrote: is this the case that as long as the virtual environment is active, the import statement will go to the installed locations of packages inside the virtual environment instead of the default PATH?
Yes.
(Mar-04-2021, 12:23 AM)leodavinci1990 Wrote: And can I import the ones inside where Python is currently installed whilst the virtual environment is active?
No the point is that is that OS Python installation and virtual environment is completely separate.
Quote:If I create a directory and activate a virtual environment inside it and then use PIP to install some packages.
It's one operation with build in venv,also it will create the folder.
# Make 
G:\div_code
λ python -m venv new_env

# Cd in
λ cd new_env\

# Activate
G:\div_code\new_env
λ G:\div_code\new_env\Scripts\activate

# Install
(new_env) G:\div_code\new_env
λ pip install requests
Collecting requests ..... 
Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.3

# List packages in environment 
(new_env) G:\div_code\new_env
λ pip list
Package    Version
---------- ---------
certifi    2020.12.5
chardet    4.0.0
idna       2.10
pip        20.2.3
requests   2.25.1
setuptools 49.2.1
urllib3    1.26.3

# Show
(new_env) G:\div_code\new_env
λ pip show requests
Name: requests
Version: 2.25.1
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: g:\div_code\new_env\lib\site-packages
Requires: idna, certifi, chardet, urllib3
See that in show it point to location of this folder and not OS Python Path.