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
  I cannot create a virtual environment on visual studio code using python Willem_Aucamp316 2 3,338 Nov-27-2024, 02:20 PM
Last Post: menator01
  How to enable Matplotlib within virtual environment? tetris_theMovie 1 1,097 Oct-11-2024, 09:56 AM
Last Post: christie2
  Need help setting up trusted root ca in virtual environment dstempfley 0 1,236 May-31-2024, 03:14 PM
Last Post: dstempfley
  Help with creating folder and "virtual environment" AudunNilsen 1 1,014 Mar-21-2024, 04:41 AM
Last Post: deanhystad
  Installing python packages in a virtual environment Led_Zeppelin 1 1,613 Aug-15-2023, 08:18 PM
Last Post: deanhystad
  Problem with virtual environment standenman 2 2,199 Feb-23-2023, 07:09 PM
Last Post: standenman
Question Virtual Environment (using VS Code) Ashcora 4 68,752 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 2,283 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  Project structure with a virtual environment gdbengo 1 2,231 Jan-26-2022, 03:22 PM
Last Post: snippsat
  Cryptic Error with import statement Led_Zeppelin 2 3,600 Jan-11-2022, 01:13 PM
Last Post: Led_Zeppelin

Forum Jump:

User Panel Messages

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