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
  Help with creating folder and "virtual environment" AudunNilsen 1 171 Mar-21-2024, 04:41 AM
Last Post: deanhystad
  Installing python packages in a virtual environment Led_Zeppelin 1 711 Aug-15-2023, 08:18 PM
Last Post: deanhystad
  Problem with virtual environment standenman 2 944 Feb-23-2023, 07:09 PM
Last Post: standenman
Question Virtual Environment (using VS Code) Ashcora 4 12,484 Feb-15-2023, 07:17 PM
Last Post: snippsat
  How do I link the virtual environment of that project to the 3.9.2 version of python? Bryant11 1 1,331 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  Project structure with a virtual environment gdbengo 1 1,389 Jan-26-2022, 03:22 PM
Last Post: snippsat
  Cryptic Error with import statement Led_Zeppelin 2 2,478 Jan-11-2022, 01:13 PM
Last Post: Led_Zeppelin
  getting an import statement to work in my program barryjo 1 1,615 Dec-06-2021, 04:28 PM
Last Post: snippsat
  Virtual environment path? Led_Zeppelin 1 2,261 Jul-22-2021, 01:40 PM
Last Post: snippsat
  virtual environment heye 4 2,398 Jul-04-2021, 06:47 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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