Suppose I have a python interpreter with many modules installed on my local system, and it has been tuned to just work.
Now I want to create a virtualenv to freeze these, so that they won't be broke by upgrading in the future.
How can I make it? Thanks.
I can't use pip freeze, because that's a cluster on which there's no pip and I don't have the privileges to install it. And I don't want the reinstall the modules either, I'm looking for that whether there's a cloning way.
Run pip freeze to create a list of all modules currently installed on the system. Then make a virtualenv and install these modules.
pip freeze > env_modules.txt
virtualenv my_env && cd my_env && source bin/activate
pip install -r ../env_modules.txt
Virtualenv does not work because it uses local python interpreter.
My solution is to use conda (anoconda or miniconda) to build the environment, so if you need some packages, you can just conda install them. Then copy it to the remote machine and run.
I think the best is to use cpvirtualenv like this:
cpvirtualenv <name_of_virtualenv_to_be_copied> <name_of_new_virtualenv>
Related
I'm looking for a way to make a virtualenv which will contain just some libraries (which I chose) of the base python installation.
To be more concrete, I'm trying to import my matplotlib to virtualenv during the creation of virtualenv. It can't be installed efficiently with pip or easy_install since it misses some fortran compiler libs. The way I did it until now was to manually copy from:
/usr/lib/python2.7/dist-packages/ to virtualenv_name/lib/python2.7/dist-packages/
However this prevents the manully imported links to be registerd by yolk (which prints all currently available libs in virtualenv).
So, is there a way to do a selective variant of the
virtualenv --system-site-packages
Create the environment with virtualenv --system-site-packages . Then, activate the virtualenv and when you want things installed in the virtualenv rather than the system python, use pip install --ignore-installed or pip install -I . That way pip will install what you've requested locally even though a system-wide version exists. Your python interpreter will look first in the virtualenv's package directory, so those packages should shadow the global ones.
You can use the --system-site-packages and then "overinstall" the specific stuff for your virtualenv. That way, everything you install into your virtualenv will be taken from there, otherwise it will be taken from your system.
I am late to the game using python.3.8 and pip3 on Ubuntu 20.04.
The ONLY way to get rid of the annoying .local install for me was to set an environment variable (bash):
export PYTHONNOUSERSITE="true"
This does not need to be "true" anything will work. I would not go for a 0. ;-)
Install virtual env with
virtualenv --system-site-packages
and use pip install -U to install matplotlib
I am trying to install numpy, nltk, etc packages for Python 2 to run a code. But I have Python3 as well and the path variable is set to it. When I try to use any pip install command it shows the package is available in Python3's directory.
Also, I am using VSCode, so I did not add the path variable.
I suggest you use virtual environments. Because if you read about virtual environments, you will find that they are created for such cases.
To create virtual environments, you must do the following:
Make a note of the full file path to the custom version of Python you just installed.
virtualenv -p /home/username/opt/python-2.7.15/bin/python venv
In order to use this environment’s packages/resources in isolation, you need to “activate” it. To do this, just run the following:
source venv/bin/activate (Linux)
./venv/Scripts/activate.bat (Windows)
Notice how your prompt is now prefixed with the name of your environment (venv, in our case). This is the indicator that venv is currently active, which means the python executable will only use this environment’s packages and settings.
Now run the following:
(venv) $ which python
/Users/ashkan/python-virtual-environments/venv/bin/python (in my case)
now you have access to python2.7.
The best practice for this particular problem would be virtual environments.And for that matter Pipenv would be a good option.
Install Pipenv.
$ brew install pipenv (MacOs)
$ sudo apt install pipenv (Debian)
$ sudo dnf install pipenv (Fedora)
pip install pipenv (Windows)
Creating virtual env with Pipenv.
pipenv install --python 2.7 numpy
This command will install create a virtual environment and install python 2.7(which will be used as the main interpreter once you activate the environment) along with numpy in that environment. This will avoid the packages version conflicts too.
To activate the environment
pipenv shell
If you are working in the Vs Code workspace then you should set the interpreter path(python path) to the path of the virtual environment.
when we install anything using pip. it will install dependencies for default python version. so you can change the default python version using this link https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux
Hope this will solve your problem
After crating a virtual environment with python 2.7 you can install your required packages
I have a Ipython Notebook that I'd like to share with others, and it uses a lot of packages.
I'm wondering if there is any tool for installing packages with ease? So others won't need to run pip install for each packages that I listed.
In Ruby on Rails, there is a gemfile, I can just run bundle install and then all gem are installed, which saves a lot of time.
I'm wondering if there is a gemfile and bundle install for ipython notebook? So we can install packages with ease.
One way to do it is to use pip:
pip freeze > requirements.txt
You could distribute that with the notebook. Then to use it:
pip install -r requirements.txt
Here is the how to with requirements.txt method. But the basic workflow is above.
As a note the requirements file will look something like:
requests==2.8.1
SQLAlchemy==0.9.9
stripe==1.27.1
Werkzeug==0.10.4
wheel==0.26.0
WTForms==1.0.5
Where you can see there are versions for each package. This method handles dependencies as well. So if one package depends on another, pip installs in such a way that there shouldn't be any errors. Though it might not always be the case.
This method should work in a Windows Powershell, definitely does in Mac and Linux.
Another is with conda:
conda create -n myenv python==3.5.0
This creates a conda environment. Which can be activated or deactivated. If activated you can install from a dependency file as:
conda env create -f requirements.yml
Similarly the requirements.txt can be created as:
conda env export > requirements.yml
For scientific applications conda is probably the best option. It allows to install from a file of package names:
conda install --file file_with_package_names.txt
Furthermore, it offers virtual environments that are more powerful than the standard virtual env:
conda create -n my_new_env python=3.5
Activate this environment:
source conda activate my_new_env
Get help with:
conda -h
List all installed packages:
conda list
List all conda environments:
conda info -e
It offers much more and works on all major operating systems. All installs are binary. So no compilation of extensions. Makes Windows users very happy. But is great for Linux/Mac folks too.
This is a nice comparison of conda, pip, and virtualenv.
I would like to install the python-numpy in the Virtualenv environment. My system is Ubuntu 12.04, and my python is 2.7.5. First I installed the Virtualenv by
$ sudo apt-get install python-virtualenv
And then set up an environment by
$ mkdir myproject
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python
Installing distribute............done.
Activated it by
$ . venv/bin/activate
Installed python-numpy in the environment by
$ sudo apt-get install python-numpy
However, I tried to import numpy in python in the environment after all steps above. Python told me "No modules named numpy". Whereas, numpy could be imported in Python globally. I tried to remove and install many times but it does not work. I am a beginner of both Python and Linux.
apt-get will still install modules globally, even when you're in your new virtualenv.
You should either use pip install numpy from within your virtual environment (easiest way), or else compile and install numpy from source using the setup.py file in the root of the source directory (slightly harder way, see here).
I'd also thoroughly recommend you take a look at virtualenvwrapper, which makes managing virtual environments much friendlier.
Edit:
You should not be using sudo, either to create your virtual environment or to install things within it - it's a directory in your home folder, you don't need elevated permissions to make changes to it. If you use sudo, pip will make changes to your global site packages, not to your virtual environment, hence why you weren't able to install numpy locally.
Another thing to consider is that by default, new virtualenvs will inherit from the global site-packages - i.e. if Python can't find a module locally within your virtualenv, Python will also look in your global site packages *. In your case, since you'd already installed numpy globally (using apt-get), when you then try to pip install numpy in your virtual environment, pip sees that numpy is already in your Python path and doesn't install it locally.
You could:
Pass the --no-site-packages option when you create your virtualenv. This prevents the new virtualenv from inheriting from the global site packages, so everything must be installed locally.
Force pip to install/upgrade numpy locally, e.g. using pip install -U --force numpy
* As of v1.7, the default behaviour of virtualenv is to not include the global site-packages directory. You can override this by passing the --system-site-packages flag when creating a new virtual environment.
meddling with PYTHONPATH for site-packages indeed defeats the purpose of virtalenv. what worked for me was to specify the env i wanted the packages to be installed in via pip
example:
pip -E /home/proj1
where proj1 was created using virtualenv.
reference: how to install numpy in a virtualenv
I'm looking for a way to make a virtualenv which will contain just some libraries (which I chose) of the base python installation.
To be more concrete, I'm trying to import my matplotlib to virtualenv during the creation of virtualenv. It can't be installed efficiently with pip or easy_install since it misses some fortran compiler libs. The way I did it until now was to manually copy from:
/usr/lib/python2.7/dist-packages/ to virtualenv_name/lib/python2.7/dist-packages/
However this prevents the manully imported links to be registerd by yolk (which prints all currently available libs in virtualenv).
So, is there a way to do a selective variant of the
virtualenv --system-site-packages
Create the environment with virtualenv --system-site-packages . Then, activate the virtualenv and when you want things installed in the virtualenv rather than the system python, use pip install --ignore-installed or pip install -I . That way pip will install what you've requested locally even though a system-wide version exists. Your python interpreter will look first in the virtualenv's package directory, so those packages should shadow the global ones.
You can use the --system-site-packages and then "overinstall" the specific stuff for your virtualenv. That way, everything you install into your virtualenv will be taken from there, otherwise it will be taken from your system.
I am late to the game using python.3.8 and pip3 on Ubuntu 20.04.
The ONLY way to get rid of the annoying .local install for me was to set an environment variable (bash):
export PYTHONNOUSERSITE="true"
This does not need to be "true" anything will work. I would not go for a 0. ;-)
Install virtual env with
virtualenv --system-site-packages
and use pip install -U to install matplotlib