Virtualenv says "root" instead of the name of virtualenv? - python

For a while, I have been using virtualenv's for many python projects. When I tried to activate one of my virtualenv's, I typed:
source venv/bin/activate
Which generally does the trick. However, instead of my command prompt saying:
(venv)me#example:~/
It says:
(root)me#example:~/
My python version requirement and my packages that I installed on the virtualenv are not recognized. Why does this happen?

Try:
source activate venv
(instead of source venv/bin/activate or ./venv/bin/activate.sh etc.)
THundtMac$ source activate venv
(venv) THundtMac$ source deactivate
THundtMac$ . ./venv/bin/activate
(root) THundtMac$ source deactivate
THundtMac$
I think this has to do with using Anaconda vs the pip-installed version. (I'm using the conda-installed one now.)

Once in the virtual environment bin folder, typing:
source ./activate
instead of:
source activate
solved the problem for me.
Please note that the latter command actually activate a "base" environment (called "root" if you use the pip version of virtualenv, and "base" if you use the conda version of virtualenv), and not the environment you are actually trying to activate

Related

I can't do anything with pipenv

ERROR:: --system is intended to be used for pre-existing Pipfile installation, not installation of specific packages. Aborting.
I can't use pipenv in Ubuntu18.04.
How can I fix it?
This is an open issue in the Pipenv repository: https://github.com/pypa/pipenv/issues/5052.
From the discussion in the thread, it seems to pop up when there is an existing virtualenv that was created with the same directory path. The solution mentioned in the thread is to simply remove this virtualenv which fixes the issue.
You can use the python virtual environment,
python -m venv venv # Creates virtual environment
To activate virtual environment do source venv/bin/activate
Then you can install your packages using pip install lib
To deactivate virtual environment type deactivate

Pip installs packages in pyenv root even when virtual environment is activated

I try to install a package with pip in a virtual environement previously created with venv. The Python version is managed through pyenv. I don't want to use pyenv to create the virtual environment.
The project is set up this way. To the project empty directory, I added a .python-version containing the version 3.8.2. Then I created my virtual environement using python -m venv .venv. Then I activated the environement using source .venv/bin/activate. Now the command line starts with a (.env). However, when I try to install some package with pip install some-package, the package ends up in {pyen_home}/versions/3.8.2/lib/python3.8/site-packages, instead of the virtual environment.
What's irritating is that I'm almost certain that I did manage to install package in the virtual environment that way before, but not anymore, so I don't see what I'm missing.
Content of your .python-version should be .venv.
As far as I know you should not create this file by yourself. It generated when you run pyenv local .venv. And venv activates automatically.
Also proper way to create virtual environment is pyenv virtualenv {python-version} {venv-name}. Read the docs carefully.

How do I activate python virtual environment from a different repo?

So am working in a group project, we are using python and of the code is on GitHub. My question is how do I activate the virtual environment? Do I make one on my own using the "python virtual -m venv env" or the one that's on the repo, if there is such a thing. Thanks
virtual env is used to make your original env clean. you can pip install virtualenv and then create a virtual env like virtualenv /path/to/folder then use source /path/to/folder/bin/activate to activate the env. then you can do pip install -r requirements.txt to install dependencies into the env. then everything will be installed into /path/to/folder/lib
alteratively, you can use /path/to/folder/bin/pip install or /path/to/folder/bin/python without activating the env.
Yes, you'll want to create your own with something like: python -m venv venv. The final argument specifies where your environment will live; you could put it anywhere you like. I often have a venv folder in Python projects, and just .gitignore it.
After you have the environment, you can activate it. On Linux: source venv/bin/activate. Once activated, any packages you install will go into it; you can run pip install -r requirements.txt for instance.

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

virtualenv not pointing to local executables

I've noticed strange behavior of virtualenv installed on my local machine. This is what I've been doing:
tomasz#laptop:~/Development/Python$ virtualenv nac-env
New python executable in nac-env/bin/python
Installing Setuptools...................................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
tomasz#laptop:~/Development/Python$ cd nac-env
tomasz#laptop:~/Development/Python/nac-env$ source ./bin/activate
(nac-env)tomasz#laptop:~/Development/Python/nac-env$ which python
/usr/bin/python
(nac-env)tomasz#laptop:~/Development/Python/nac-env$ which pip
/usr/bin/pip
(nac-env)tomasz#laptop:~/Development/Python/nac-env$ which easy_install
/usr/local/bin/easy_install
(nac-env)tomasz#laptop:~/Development/Python/nac-env$ echo $PYTHONPATH
/usr/lib/python2.7/site-packages:/usr/lib/python2.7/dist-packages
(nac-env)tomasz#laptop:~/Development/Python/nac-env$ deactivate
tomasz#laptop:~/Development/Python/nac-env$
As the nettuts screencast says (http://www.youtube.com/watch?v=IX-v6yvGYFg, 6:55), python, pip and easy_install executables should point to files inside the virtual environment and not to /usr/bin/... stuff. If the $PYTHONPATH sysvar is set incorrectly, I think that my installation doesn't work as expected... Does anyone know what's going wrong?
PS I have installed virtualenv globally on my local machine some time ago. Probably it was intalled through pip or sth like that.
Well, I made myself sure this behavior I had in fact is not normal. I've uninstalled existing virtualenv and re-installed it from pip and now everything works perfectly:
tomasz#laptop:~/Development/Python/foo$ source bin/activate
(foo)tomasz#laptop:~/Development/Python/foo$ which python
/home/tomasz/Development/Python/foo/bin/python
(foo)tomasz#laptop:~/Development/Python/foo$ which pip
/home/tomasz/Development/Python/foo/bin/pip
(foo)tomasz#laptop:~/Development/Python/foo$ which easy_install
/home/tomasz/Development/Python/foo/bin/easy_install
(foo)tomasz#laptop:~/Development/Python/foo$ echo $PYTHONPATH
/usr/lib/python2.7/site-packages:/usr/lib/python2.7/dist-packages
I had this happen to me. Did you change your project path?
When I first created the virtual environment, I used ~/src/my_project/venv but later changed it to ~/projects/my_project/venv. When I ran the activate scripts, it was using the old path. To fix this, I did a substitution on all of the scripts in the activate directory with the new path and python, pip, etc. were now sourced from the virtual environment.

Categories