Virtualenv installing to local instead of virtual - python

I am trying to install OpenERP server into a virtual enviroment that I created for that. I created the virtual using
mkvirtualenv openerp_rev5054
On said virtual enviroment I use
> (openerp_rev5054)user#machine:python setup.py install --record files.txt
And gives that I have no permission, and if I install it with root access, it goes directly into my python local install. Here is a sample of my files.txt with the location of all the files installed.
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/import_xml.rng
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/release.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/netsvc.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/loglevels.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/__init__.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/sql_db.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/release.py
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/http.py
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/PKG-INFO
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/pooler.py
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/loglevels.py
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/pooler.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/__init__.py
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/exceptions.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/sql_db.py
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/http.pyc
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/netsvc.py
/usr/local/lib/python2.7/dist-packages/openerp-7.saas_3-py2.7.egg/openerp/exceptions.py
I have a couple of others already installed and the procedure was the same so I see no reason why this one should behave different in any way. Has anyone encountered an issue like this?

Try installing with pip rather than setup.py. I hear that pip plays nicer with virtual environments.

Check to make sure that your virtualenv is activated. If it is, you should have an environment variable $VIRTUAL_ENV that has the value of the root directory of the virtualenv. Or, more commonly, you'll see a change in your shell prompt.
If it is activated, make sure that your $PATH is correct, perhaps something else has come along and changed it. which python should point at a python inside your virtualenv bin directory.
If you have an old version of virtualenv, ensure you're using --no-site-packages

Related

What is the best way to upgrade python in a virtual environment using virtualenvwrapper?

I created an environment using virtualenvwrapper while my machine was running Python 3.8.3. Naturally, this made the environment with Python 3.8.3 as well.
Later, I updated Python on my main machine using home brew to 3.10.
Now I have 3.10-specific code in the virtual env project, running 3.8.3.
The first entry in that project's $PATH is set to the virtual env itself, which uses the old Python. The 2nd entry is Python 3.10. I believe this is standard. The virtual env itself is added to the front of $PATH by virtualenverapper upon activation.
Short of manually manipulating the .zprofile or a virtalenvwrapper's postactivate script, I am wondering if there is a more sweeping and automatic way of updating a virtual environment using virtualenvwarpper?
I am no expert on this stuff, so my concern is that manually manipulating files, or applying one-time fixes will just be asking for trouble down the line.
Thanks for your help.
EDIT: I wanted to add that I am also learning git and have a repo set up in this project. I would like to ideally preserve that information through the "upgrade," which it sounds like involves creating a new env. The virtualenvs are stored on the ~user directory in .virtualenvs. The git repo is in the project directory.
You don't want to do this. As you said, even if you pulled it off you're sure to have hidden issues that'll be a major headache down the line. Fortunately, it's very easy to recreate the virtual env with exactly the same installed packages you had before but with a new Python version.
What you want is to compile a list of installed packages in your old virtualenv, make your new venv with the desired Python version, then reinstall all the packages. We can do this simply like this :
workon oldenv
pip freeze > env_requirements.txt
deactivate
mkvirtualenv newenv -p `which python3.10`
pip install -r env_requirements.txt
If you're happy with the result, you can then delete the old venv :
rmvirtualenv oldenv
As to your concern with git, this should have absolutely no impact whatsoever on your git repo.

How to activate an existing virtualenv projects?

I'm a beginner to Django and Python, and I've never used virtualenv before. However, I do know the exact commands to activate and deactivate virtual environments (online search). However, this learning course takes time and sometimes I need to split the work over 2 days.
When I create a virtualenv today and do some work, I'm unable to access the same virtualenv tomorrow. Even when I navigate to that folder and type in .\venv\Scripts\activate, it says "system cannot find specific path".
How can I open already existing virtual environments and the projects within them? Could it be that I need to end my previous session in a certain way for me to access it the next time?
Even though pipenv had so many problems. I suggest you use it when you are new to virtual env.
Just
pip install pipenv
cd $your-work-directory
pipenv shell
Then you created your project env.
You can active it by:
cd $your-work-directory
pipenv shell
You can install packages by:
cd $your-work-directory
pipenv install $yourpackage --skip-lock
Open the command prompt
Go to the project directory, where you created virtual environment
And type the same as error shows, as in my case it was
File D:\Coding files\Python*recommendation-system\venv\Scripts\activate.ps1* cannot be loaded because running scripts is disabled on this system.
So I typed recommendation-system\venv\Scripts\activate.ps1
And it resolved my problem.
Use this and it will work:
cd $working directory u have virtual env on
pipenv shell
you can use this command it worked for me for reusing your existing venv
$ workon then the name of your existing venv

Will libraries installed to my "base" system Python be available in a virtualenv?

I'm new to python, so please be gentle.
In learning python and writing my first few scripts, I quickly glossed over any tutorial sections on virtualenv, figuring it wouldn't provide me any benefit in my nascent stage.
I proceeded to hack away, installing packages as I went with pip3 install package
Now I've built something that is potentially useful to my organization, and I'd like to share it. In this case, I want to distribute it as a windows executable.
Before building this distribution, I figure it's now time to take the next leap from individual scripts to proper python projects. It seems like virtualenv should be part of that.
Given that I've installed a number of packages to my "base" python environment: in order to do development in a "clean" virtual environment, do I need to somehow "revert" my base python environment (i.e. uninstall all non-standard packages), or will virtualenv shield a project within a virtual environment from non-standard packages installed to my "base" environment?
If you are using the venv module there is --system-site-packages flag that will grant the created virtual environment access to the system-wide site-packages directory:
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
Go install VirtualEnvWrapper first. After that, create a new virtualenv, activate it, and run pip freeze. You should see nothing in there because nothing is installed. Deactivate the env to go back to your 'Base' environment and pip freeze again. You will see all the installs you have.
A best practice is to create a requirements.txt file and version control it so everyone can use the same versions of the same packages. If you don't want to do this, simply activate your new virtual env and pip install everything you want.
You can specify separately the required libraries and check if they are installed and if not then you can install them automatically.
Have a look at:
https://packaging.python.org/discussions/install-requires-vs-requirements/

Python system libraries leak into virtual environment

While working on a new python project and trying to learn my way through virtual environments, I've stumbled twice with the following problem:
I create my virtual environment called venv. Running pip freeze shows nothing.
I install my dependencies using pip install dependency. the venv library starts to populate, as confirmed by pip freeze.
After a couple of days, I go back to my project, and after activating the virtual environment via source venv/bin/activate, when running pip freeze I see the whole list of libraries installed in the system python distribution (I'm using Mac Os 10.9.5), instead of the small subset I wanted to keep inside my virtual environment.
I'm sure I must be doing something wrong in between, but I have no idea how could this happen. Any ideas?
Update:
after looking at this answer, I realized the that when running pip freeze, the pip command that's being invoked is the one at /usr/local/bin/pip instead of the one inside my virtual environment. So the virtual environment is fine, but I wonder what changes in the path might be causing this, and how to prevent them to happen again (my PYTHONPATH variable is not set).
I realized that my problem arose when moving my virtual environment folder around the system. The fix was to modify the activate and pip scripts located inside the venv/bin folder to point to the new venv location, as suggested by this answer. Now my pip freeze is showing the right files.

pip-python not found within virtual environment

I've installed python-virtualenv and python-virtualenvwrapper, and created a virtual environment by using mkvirtualenv NAME, and then activated it through workon NAME. By looking in ~/.virtualenvs/NAME/bin I see that pip is installed there.
However, when I try and install anything through pip, I'm told pip-python: command not found
I have not installed pip system wide, and was under the impression that I did not need to, given that it was already installed inside the virtual environment. Now, all this leads me to believe that something is not being set correctly with my $PATH, what could that be though? Once I'm in side the virtual environment as such: (NAME)[user#host]$ shouldn't my path already be modified to use the pip installation inside that environment? What do I need to do to make this so?
You must install pip on you system to make it accessible in virtualenv.
pip-python is the name of the executable in some Linux distributions. It is on my Fedora machine.
When pip is installed in a virtualenv, the name of the executable is simply pip, not pip-python. So you need to execute it with ~/.virtualenvs/NAME/bin/pip, not ~/.virtualenvs/NAME/bin/pip-python.

Categories

Resources