I created a new VENV and pip installed awscli there.
I can see it installed with pip list --local.
But when I type aws - it does not recognize the command (even though I activated that VENV).
How to setup paths so that newly installed library in VENV can be recognized?
There are many ways to create a virtual environment. Let's say:
python3 -m venv my_venv
But to install some package in that particular environment, you have to activate it by:
For windows:
my_venv\Scripts\activate
For Linux or Mac:
source my_venv/bin/activate
Then if you install a package by using pip, it will be installed in that particular virtual environment. Otherwise, it will be installed in the local environment.
Related
I have been through numerous similar questions and I still don't understand how are we activating virtual environment for Django projects. Please mention explanation for how each command works and one more questions is that why do we not need to install python in Django's virtual environment, I am getting confused. Thank you in advance, please help this newbie.
Benefits
You can use any version of python you want for a specific environment
without having to worry about collisions (shoutout to my python 2.7
mac users!)
You can organize your packages much better and know exactly the
packages you need to run your code incase someone else needs to run
it on their machine
Your main python package directory does not get FLOODED with
unnecessary python packages
To create virtual environment
step 1 install environment package (virtualenv) using pip
pip install virtualenv
step 2 create virtualenv
virtualenv env_name #<- env_name is virtualenv name you can set any
step 3 Activate Virtual env
env_name\Scripts\activate #<- for window
step 4 Install pakages you wan to install in virtual env
cmd(env_name): pip install django
Note that python is install in your virtual env automaticaty the
version is same as in your local machine
There is no difference between activating virtual environment for Django or for other purposes. Django on it's own does not differ from any Python library out there.
Python virtual environment allows you to separate your system Python and it's libraries and create self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
On Linux, assuming you already have Python 3 and pip3 installed:
# install virtualenv package (skip if you have it already)
pip3 install virtualenv
# create virtual environment in directory "tutorial-env"
python3 -m venv tutorial-env
# activate virtual environment
source tutorial-env/bin/activate
Upon activation below command should give path to new Python binary:
which python3
Similarly with pip3
which pip3
As long as your environment is activate you can run pip3 install $package_name and it will install it inside virtual environment.
To deactivate your virtual environment:
deactivate
For more info and commands for Windows:
https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
https://docs.python.org/3/tutorial/venv.html
I have set up a virtual environment correctly have activated it and when I do "which python" it tells me the correct directory, but when I do pip install in the venv it installs the package in the default directory of the mac and not in my venv. I have tried using pycharm and installing packages with that, but it happens the same thing.
Edit:
Following I will do a walkthru of my steps, first I did python3 -m venv /path/to/new/virtual/environment, then I did source env/bin/activate, then I did which python and I got the intended directory, afterwards I did pip3 install numpy and I saw the installation process, then I did pip list and numpy was not there, I checked the directory manually and it still was not there. I retried all the same thing with pycharm with the same results.
Follow these steps in order -
In your current working directory :
python3 -m venv venv
source venv/bin/activate
(venv) pip3 install library_name
To check if libraries were properly installed or not :
(venv) pip3 freeze
and check if your installed libraries is displayed on the terminal screen or not.
To deactivate the virtual environment :
(venv) deactivate
This is due to permission issues
Run this command to change permission of your virtual environment folder
sudo chmod -R 777 venv
After activating your virtual environment
source venv/bin/activate
Then try to install Django by either Python3 or Python2
pip3 install django or pip install django
Then check the list of installed packages by either Python3 or Python2
pip3 list or pip list
NOTE: If you run sudo pip3 install django or pip install in virtual environment that is not writeable by default it will be installed outside of virtual environment
If you run pip3 install django or pip install in virtual environment that is not writtable by default it will be installed outside of virtual environment you will get OS Persmission Error
I have used pip3 and pip because you can install packages by either Python3 or Python2, thing to remember if you created virtual environment by Python3, use pip3 and if you created by using Python2 use pip.
I'm working with a distributed package system (happens to be CVMFS but I don't think the particulars are relevant). The setup script for that environment adds some locations to PYTHONPATH.
Now, working in that environment, I want to install newer versions of some packages that are already found within that path. Installing the packages is easy, I can either do
python -m pip install --user --upgrade <packages>
or, my preferred approach, use a virtual environment
python -m venv myenv
source myenv/bin/activate
python -m pip install --upgrade <packages>
Installation works fine. But then when I try to run python, the directories in PYTHONPATH are search before either my user-site directory or the virtual environment, and I get the old versions of packages.
Is there any way to force packages in my virtual environment or user-site path to have priority, without having to manually edit sys.path or PYTHONPATH?
I have activated the python virtual environment, but anyway when I run pip install *, the dependencies are installed to my local Python path. The same happens when I run the server in my Django project: python manage.py runserver -> the system is not using the virtual environment, but the python from my PC. What is the problem? Why my activated virtual environment does not work?
I am using MacOS. Everything worked until I erased all my data and installed again Python.
Thank you!
If the packages aren't getting installed within your virtual environment, it most likely is the case that you are not using the pip within your virtual environment (or may not have pip within your virtual environment at all, in which case it is defaulting to use the pip installed in /usr/local/bin/). First, check that you have a separate version of pip in your virtual environment located in ./your_virtual_environment_name/bin/pip....If you don't, install from the PyPA download page (https://pip.pypa.io/en/stable/installing/#installing-with-get-pip-py). When you go to install a package within your virtual environment, activate the virtual environment first using source ./bin/activate inside your virtual environment, then "cd .." and you'll use the pip installed within the virtual environment by typing: ./your_virtual_environment_name/bin/pip install * ....The installation will be in ./your_virtual_environment_name/lib/python3.6/site-packages/
I made Virt2 virtual environment.
using $ python -m venv Virt2.
I want to install my custom packages in "site-packages" directory. However packages are installed in "dist-packages" directory.
what should I do to install packages in my python virtual environment site-packages??
my python version 3.6.2 (in /usr/local/bin)
You use sudo and sudo switches user to root, i.e. you're completely outside of you virtual env. System pip3 outside of virtual env installs packages into a system directory which is dist-packages.
Run pip install inside you virtual env without sudo.