virtualenv not copying the existing python modules - python

python -c 'import pyscopg2'
this works fine.
Now I create a virtualenv
virtualenv venv
and activate it
source venv/bin/activate
and run the following
python -c 'import pyscopg2'
But it gives error 'ImportError: No module named pyscopg2'

You should create virtual environment using --system-site-packages option to access system package.
According to virtualenv --help:
--no-site-packages Don't give access to the global site-packages dir to
the virtual environment (default)
--system-site-packages
Give access to the global site-packages dir to the
virtual environment

Related

virtualenv in python : can't create full files and activate not working

I am creating a new python environment by using command virtualenv myenv. The command gets executed successfully. But whenever I try activating the environment I get nothing (no activation is taking place) when I open the created folder, the files available in virtual env are libs, scripts and pyvenv.cfg only. Nothing else other than these 3.
Try to create your environment with venv:
python3 -m venv /path/to/new/virtual/environment
And activate it normally.
In windows, you can use in the cmd :
py -m venv env
and activate it :
.\env\Scripts\activate

Get path to original python executable (not the virtualenv)

Inside a virtualenv, sys.executable gives /path/to/venv/bin/python. How do I get the path of the python that the virtualenv was created from, such as /usr/bin/python3?
For example,
$ cd /tmp
$ virtualenv -p /usr/bin/python3 venv
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /tmp/venv/bin/python3
Also creating executable in /tmp/venv/bin/python
Installing setuptools, pip, wheel...
done.
$ venv/bin/python -c 'import sys; print(sys.executable)'
/tmp/venv/bin/python
The answer that I want is /usr/bin/python3.
In a virtualenv, sys.real_prefix is the directory of the actual Python installation being used by the virtualenv.
Other variables worth checking are sys.base_prefix and sys.base_exec_prefix.
Example code to run in virtualenv:
import sys
print(sys.base_prefix)

How to active a virtualenv with pyenv installed

I use pyenv for handling my python versions. I also use virtualenv for creating my virtual environments. In a new project, I made a new virtual environment with python -m venv venv to make a directory called venv that has a fresh python 3.6. I then did source venv/bin/activate expecting that to make venv/bin/python to be my default python from this directory, but when I do command -v python, I am told that /Users/TheStrangeQuark/.pyenv/shims/python is still the path. I also checked with python -c "import sys; print(sys.executable)" and was printed with /Users/jhuneau/.pyenv/versions/3.6.2/bin/python.
Why is source venv/bin/activate not working to set the python executable in this directory to use the virtual environment?
I think you should try:
source venv/bin/activate
instead of
source venv/bin/active

How to create virtual env with python3

I am using python 2.7 + virtualenv version 1.10.1 for running myproject projects. Due to some other projects requirement I have to work with other version of python(Python 3.5) and Django 1.9. For this I have installed python in my user directory. Also I have dowloaded and installed virtualenv( version - 15.1.0) into my user directory.
But whenever I am trying to create virtual env I am getting the below error
python virtualenv/virtualenv.py myproject
Using base prefix '/home/myuser/python3'
New python executable in /home/mount/myuser/project_python3/myproject/bin/python
ERROR: The executable /home/mount/myuser/project_python3/myproject/bin/python is not functioning
ERROR: It thinks sys.prefix is '/home/myuser/python3' (should be '/home/mount/myuser/project_python3/myproject')
ERROR: virtualenv is not compatible with this system or executable
Can anybody tell what I am doing wrong with this
In Python 3.6+, the pyvenv module is deprecated. Use the following one-liner instead:
python3 -m venv <myenvname>
This is the recommended way to create virtual environments by the Python community.
To create virtual env
virtualenv -p python3 venv_name
This will create new python executable in baseDirectory/bin/python3
How to activate newely created Venv:
cd baseDirectory/bin/
source activate
Deactivate new venv
deactivate
UPDATE_1
This method has been depreciated as The use of venv is now recommended for creating virtual environments.
Please check this link for updated answer
Python already ships with its builtin "virtualenv" called venv since version 3.3. You no longer need to install or download the virtualenv scripts for Python 3.3+.
https://docs.python.org/3/library/venv.html
Check that your installation provided the pyvenv command that should take care of creating the "virtualenv". Arguments are similar to the classic virtualenv project.
$ pyvenv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
[--upgrade] [--without-pip]
ENV_DIR [ENV_DIR ...]
Creates virtual Python environments in one or more target directories.
positional arguments:
ENV_DIR A directory to create the environment in.
optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks
are not the default for the platform.
--copies Try to use copies rather than symlinks, even when
symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it
already exists, before environment creation.
--upgrade Upgrade the environment directory to use this version
of Python, assuming Python has been upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual
environment (pip is bootstrapped by default)
Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.
virtualenv is the tool of choice for Python 2, while venv handles the task in Python 3.
Yet you can create the virtual environment for Python 3 using any of them.
Using venv
python3 -m venv virtualenvname
Command Syntax:
/path/to/python3 -m venv /path/to/directory/virtual_env_name
Using virtualenv
virtualenv -p python3 virtualenvname
Command Syntax:
virtualenv -p /path/to/python3 /path/to/directory/virtual_env_name
Activate the virtual environment
On Linux, Unix or MacOS, using the terminal or bash shell:
source /path/to/venv/bin/activate
e.g. source virtualenvname/bin/activate
On Unix or MacOS, using the csh shell:
source /path/to/venv/bin/activate.csh
On Unix or MacOS, using the fish shell:
source /path/to/venv/bin/activate.fish
On Windows using the Command Prompt:
path\to\venv\Scripts\activate.bat
On Windows using PowerShell:
path\to\venv\Scripts\Activate.ps1
Deactivating the virtual environment
On Linux, Unix or MacOS, using the terminal or bash shell:
deactivate
On Windows using the Command Prompt:
path\to\venv\Scripts\deactivate.bat
On Windows using PowerShell:
deactivate
This answer is for those who may use a different OS.
Since the launch of Python version 3.3, there has been no need to download the virtualenv package separately as it comes built-in in Python.
Refer to the documentation to gain complete insights on it.
Test the installation of virtualenv:
$ virtualenv --version
Usage:
1.Creating a virtual environment:
$ virtualenv --system-site-packages -p python3 ./virtual_env_name
2.For enabling it, use the following command:
$ source ./virtual_env_name/bin/activate
3.For disabling the virtual environment and get back to working with the local environment:
$ deactivate
For listing down the packages in the virtual environment, use the
following command:
$ pip3 list
I install it using the command (for Python 3.x),
$ python3 -m venv env
To create a virtual environment in python3:
virtualenv -p /usr/bin/python3 virtualenvname
After creating the virtual environment, we need to activate it using the below command:
source virtualenvname/bin/activate
to deactivate use the below command:
deactivate
If you are on windows.
manually download and install the version of python you want from the official site
after installation, search "python" to locate the folder, so you can identify the path
get the path of the .exe (for example: C:\Users\Path\Programs\Python\Python38\python.exe)
Inside the folder of which you want to create the environment...start bash or VSCode terminal, or whatever command prompt, type [python .exe path] -m venv [env name] like this:
C:/Users/Path/Programs/Python/Python38/python.exe -m venv myenv
(Note that you have to change forward slash to backward slash for the path on step 4)
Activate the environment like so:
source myenv/Scripts/activate
Install virtualenvwrapper on top of virtualenv to simplify things.
Follow the blog to install in easy steps: virtualenvwrapper
Steps to create it:
mkvirtualenv -p /usr/bin/python3
Install packages using - pip install package_name
workon - activates the virtualenv, deactivate - deactivates the viirtualenv

How does pip know about virtualenv and where to install packages

I am familiar with how a python virtualenv works and when we activate, it mainly modifies VIRTUAL_ENV and PATH variable. Primarily it adds the bin directory of virtualenv to system PATH and sets VIRTUAL_ENV to point to the virtual env root directory.
As an experiment I did this using virtualenv (version 1.11.6):
# Created two fresh virtual environments
virtualenv /tmp/env1
virtualenv /tmp/env2
echo $PATH
> SOME_PATH_VALUE
# Play with env1
source /tmp/env1/bin/activate
echo $PATH
> /tmp/env1/bin:SOME_PATH_VALUE
pip list
> pip, setuptools, wsgiref
pip install wget
> pip, setuptools, `wget`, wsgiref
# Obviously env2 has pip, setuptools and wsgiref only as of now
export PATH=/tmp/env2/bin:$PATH
pip list
> pip, setuptools, wsgiref
With my experiments it was solely depending on PATH variable for determining the installation path, but I am not sure if thats the whole picture. So the key questions are:
How does pip know where to install the requested package?
If my virtualenv is configured not to look for site-packages (ensured that the no-global-site-packages.txt file exists in correct location) and PATH variable is correctly set, but pip install still looks into system site-packages, then how to debug this?
Virtualenvs have the pip command installed in their bin/ directories. By swapping out the PATH you are executing a different pip command each time.
The bin/pip script is tied to the bin/python executable for that virtual env, and in turn that bin/python executable is tied to the virtualenv; you don't have to use source bin/activate to be able to use a virtualenv, because just the bin/python executable has all the information it needs.
You can see this by looking at the sys.prefix variable:
$ virtualenv-2.7 /tmp/env1
New python executable in /tmp/env1/bin/python2.7
Also creating executable in /tmp/env1/bin/python
Installing setuptools, pip...done.
$ virtualenv-2.7 /tmp/env2
New python executable in /tmp/env2/bin/python2.7
Also creating executable in /tmp/env2/bin/python
Installing setuptools, pip...done.
$ env1/bin/python -c 'import sys; print sys.prefix'
/private/tmp/env1
$ env2/bin/python -c 'import sys; print sys.prefix'
/private/tmp/env2

Categories