Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
virtual env
#1
Hello,
I want to use virtualenv with python.
I work with Windows 7 and python 3.7, i developped with the idle editor.

i create with virtualenv a virtual environnement and i would like to:
1) Put some python file in this virtual environnement
2) import some standard library in my environnement if not done yet

There are a lot of thing on the net but nothing really clear for me.
Any help?
Thanks
Reply
#2
I have a tutorial here.
There also look at setup cmder which you should use.

Can show a run from cmd,venv is build into Python.
# Test python
C:\>python -c "import sys; print(sys.executable)"
C:\python37\python.exe

# Test pip
C:\>pip -V
pip 19.0.3 from c:\python37\lib\site-packages\pip (python 3.7)

# Make virtual enviroment 
C:\1>python -m venv test_env

# Cd in
C:\1>cd test_env

# Activate enviroment 
C:\1\test_env>C:\1\test_env\Scripts\activate

# Test python again,see now that it point to enviroment folder
(test_env) C:\1\test_env>python -c "import sys; print(sys.executable)"
C:\1\test_env\Scripts\python.exe

# Test pip,see that it point to enviroment folder
(test_env) C:\1\test_env>pip -V
pip 19.0.3 from c:\1\test_env\lib\site-packages\pip (python 3.7)

# Will now only install to enviroment 
(test_env) C:\1\test_env>pip install requests
Collecting requests
......  
Installing collected packages: urllib3, certifi, chardet, idna, requests
Successfully installed certifi-2019.3.9 chardet-3.0.4 idna-2.8 requests-2.21.0 urllib3-1.24.2

# Test that it work
(test_env) C:\1\test_env>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> exit()

(test_env) C:\1\test_env>
Using from editor it most point to python.exe in environment folder.
VS Code from start will find virtual environment,or can just start from folder with code .
Also for basic setup look at Python 3.6/3.7 and pip installation under Windows.
Reply
#3
Thanks!
But sorry it is not clear for me again.
I have created a virtual environement ('c:\Virtual_Env')and i know how to install new module in my virtual env.

1)i want to import standard lib of python (maybe it is already done when the virtual env is created?)
2) i want to activate my virtual env in my python code i want to run
3) when i close my python code, i want to automatically switch off my virtual env in my python code

3b) when i do a pip freeze > requirement.txt i save all my module i have installed in my virtual env but what about the version of python and its associated standard module?

Thanks
Reply
#4
(Apr-19-2019, 07:24 AM)jeuvrey Wrote: 1)i want to import standard lib of python (maybe it is already done when the virtual env is created?)
A virtual environment is a full clone of Python,then all of standard lib is there.
(Apr-19-2019, 07:24 AM)jeuvrey Wrote: 2) i want to activate my virtual env in my python code i want to run
There no need to activate other than when installing,when use from a editor it most use python.exe in the environment folder.
Editor like VS Code(as mention),PyCharm do find virtual environment.
As soon as you exit Terminal the virtual environment is not activate,you see in my code that (test_env) show first when active.
Quote:3b) when i do a pip freeze > requirement.txt i save all my module i have installed in my virtual env but what about the version of python and its associated standard module?
You can just tell in doc that this package need eg Python 3.6 or higher.
Can put in Environment Markers in requirements.txt.
SomeProject==1.2; python_version < '3.6'
SomeProject; sys_platform == 'win32'
Just as a note is not normal to ask people to do pip install requirement.txt.
When make a module/package you will use setup.py then eg if publish on PyPi.
Then people will do pip install my_fancy_thing,then this will install all requires module/package specified in setup.py.
Has a longer tutorial about this here.
Reply
#5
Hello,
Thanks but i use idle and i have difficulties to use virtual env:
here is my code to create a virtual env and activate it:
import sys
import os
import subprocess

''' Create virtual env in an folder'''
subprocess.run ('virtualenv test_env')

''' Activate virtual env'''

activate_env = 'P:/Mes_Doc_reseau/recherche/Programmes/python/virtualEnvTest/test_env/Scripts/activate.bat'
subprocess.run(activate_env)
print(sys.executable)   # -> not the right interpreter!!!
When i try to see the interpreter used, i see that it is not the right interpreter!!
Thanks!
Reply
#6
You need to do this from command line,using subprocess for this make no sense at all.
Do not use virtualenv(only needed for older Python version),use the build in venv.

Try to follow what i have done,i have tried to explain this with all step needed in detail(with comments) from command line(cmd or better cmder).
Reply
#7
Hello,
i just want to set a automatic script to create virtual env with eventually all the package needed in this virtual env. I work with people who are not computer scientist(like me) and need "plug-and-play" solution. Moreover i would like to run idle in the virtual env, now i know how to do it (python -m idlelib in the virtual env). Finally i have created a batch file and it seems pretty good.
Maybe i will post it when i am finished it!
Thanks!
Reply
#8
(Apr-25-2019, 12:09 PM)jeuvrey Wrote: i just want to set a automatic script to create virtual env with eventually all the package needed in this virtual env. I work with people who are not computer scientist(like me) and need "plug-and-play" solution.
Anaconda | Miniconda is made for this purpose to make it simple for computer scientist that need to run the Python scientific stack and other 3-party modules without messing with install.
Anaconda comes with 1500+ data science packages and other stuff pre-installed.
Miniconda comes only with basic stuff to install conda and pip,then can make a stand alone disruption with only packages of choice.
So a stand alone disruption means that can be run from usb stick or can just copy whole anaconda/miniconda folder over.
I have tutorial here.
Reply
#9
Thanks a lot, i will read this.
Reply


Forum Jump:

User Panel Messages

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