Python Forum
import statement in a virtual environment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import statement in a virtual environment
#1
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?
Reply
#2
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual environment and upgrading python 3.5 to 3.9 NeilUK 4 479 Jan-24-2021, 01:02 PM
Last Post: snippsat
  Not able to set up a virtual environment with venv mohanp06 7 703 Oct-27-2020, 12:18 PM
Last Post: snippsat
  Do I need to make a virtual environment? Pedroski55 5 665 Sep-21-2020, 12:10 AM
Last Post: seandepagnier
  sys.path for virtual environment ErnestTBass 2 504 Sep-10-2020, 06:36 PM
Last Post: ErnestTBass
  Cannot get to virtual environment in Jupyter IDE ErnestTBass 6 832 Sep-09-2020, 05:56 PM
Last Post: ErnestTBass
  [PyCharm] Working with virtual environment AndrzejB 1 444 Jul-18-2020, 06:22 PM
Last Post: AndrzejB
  Has anyone figured out how to get pywin32 into a virtual environment yet? RobR 6 868 Jun-04-2020, 01:26 PM
Last Post: RobR
  Unable to install virtual environment on python 3.8.3 Allen10 4 784 Jun-04-2020, 04:22 AM
Last Post: Larz60+
  Pycharm Virtual environment Bark52 0 426 May-27-2020, 02:40 AM
Last Post: Bark52
  Virtual environment base folder is not add to sys.path on Linux AbdelrahmanMSaid 0 640 Mar-16-2020, 11:44 PM
Last Post: AbdelrahmanMSaid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020