Pip gets removed from virtual env after install a package - python-3.x

I have created a virtual environment:
Python -m venv test
I then checked that pip was indeed in the environment. this is the content of the scripts folder:
activate deactivate.bat pip.exe* python.exe*
activate.bat easy_install.exe* pip3.9.exe* pythonw.exe*
Activate.ps1 easy_install-3.9.exe* pip3.exe*
I then ran which pip to confirm that it would be the env pip, which it was.
I wanted to install django:
pip install django
After i installed it, i ran which pip again, but this time it was pointing to system pip. The scripts folder in my environment no longer had the pip files.
How do i keep things separate?

Related

Setting Up a Python Virtual Environment with Hydrogen in Atom

I'm in the middle of switching from VS Code to Atom and I'm trying to set up a virtual environment for my python project.
It was pretty easy to do in VS Code, I'd run the following script and it would automagically start using the new env (with all the required packages) when I'd run the script:
python3 -m venv my_env
source my_env/bin/activate
pip3 install -r requirements.txt
Now I'm trying to set up Hydrogen to work the same way. When I run lines of code inline with Hydrogen, I want them to be run in a virtual environment that has the imported modules I need from a requirements.txt file.
I was able to install the python3 kernel with the following commands:
python3 -m venv my_environment_name # create a virtual environment
source my_environment_name/bin/activate # activate the virtual environment
python -m pip install ipykernel # install the python kernel (ipykernel) into the virtual environment
python -m ipykernel install
And Atom is able to see it:
Screenshot
However, I'm still puzzled as how to install my dependencies into the kernel. And if I do install my dependencies there, I don't want my next python projects to have all those modules in there. I'd love to have the fresh-slate that virtual environments promise.
Any help here would be appreciated. Has anyone had experience setting up a virtual environment that can be used by the Hydrogen package?
Ok, after some more experimentation, I was able to connect to a kernel that I had installed my requirements.txt into.
Here are the steps I took:
python3 -m venv env
source env/bin/activate
# make sure requirements.txt has ipykernel in it
pip3 install -r requirements.txt
python3 -m ipykernel install --user --name=env
Then in Atom, press cmd-shift-p and find Hydrogen: Update Kernels.
Or manually Packages->Hydrogen->Update Kernels
After, I was able to use the kernel by doing cmd-shift-p again and selecting Hydrogen: Start Local Kernel and selecting env.
When I would run import statements via Hydrogen (selecting them and pressing cmd-enter), they would now know what to import! Horray!
Windows Version.
This is the windows version of the excellent answer by Tim Estes. Without the use of requirements.txt in case some folks such as myself would like to install ipykernel separately. (Thanks so much Tim, it saved me a ton of time)
python -m venv env
env/bin/activate
pip install ipykernel
python -m ipykernel install --user --name=env
Similar to Tim's answer, go ahead and update kernels at Atom.
Packages->Hydrogen->Update Kernels

Python 3 - How do you re-create your Pipfile?

I am a major Python noob, and I made the mistake of manually deleting my Pipfile and Pipfile.lock, thinking that they would eventually regenerate.
How can I re-create these files?
There is a simple fix to this:
First you need to install pipenv if you haven't already:
pip install pipenv
Then change directory to the folder containing your Python project and initiate Pipenv (replace my_project with the name of your project folder):
cd my_project
pipenv install
This will create two new files, Pipfile and Pipfile.lock, in your project directory, and a new virtual environment for your project if it doesn’t exist already.
For regular pip:
pip freeze > requirements.txt
For pipenv:
pipenv run pip freeze > requirements.txt
And to install:
pip install requirements.txt
Situation: you have deleted Pipfile and Pipfile.lock, but your pipenv environment is still there. By this I mean pipenv shell puts you in your environment with a python that has all your packages right there.
# Create a new Pipfile
pipenv install
# Get a list of your packages + version from the would-be "reqiurements.txt", then install them one by one.
pipenv run pip freeze|sed 's/^/pipenv install /'|source /dev/stdin
Note that this is not a perfect solution, as it will specify the versions in the production target environment as you would get from pip freeze i.e. requirements.txt. However this has saved me more than once, so I hope it helps you.

Can I create a virtualenv after making the mistake of installing all project packages globally?

So I am a python newb like first project ever newb. I jumped right in disregarding virtualenv and installed everything globally. Now I need to be able to share my project with other team members.
Can I create a virtualenv after making the mistake of installing all project packages globally?
I am using python 3. I've read these links:
pip installing in global site-packages instead of virtualenv
How to import a globally installed package to virtualenv folder
But I don't think thats what Im looking for. I want to go the requirements.txt route I think.
Any guidance or suggestions?
Yes, you can create a virtual env.
You can create a requirements.txt file for the packages you installed globally.
pip3 freeze > requirements.txt
and then you can use this requirements file for installing all the packages in the virtual env which will be isolated from your global environment.
First you need to install virtualenv:
pip3 install virtualenv
Create a new virtual env using the following command:
virtualenv -p python3 envname
You can activate the virtualenv by:
source /path/to/new/virtual/environment/bin/activate
To deactivate the environment and return to your local environment just run:
deactivate
Install the requirements from the file.
cat requirements.txt | xargs -n 1 pip3 install
This should install all your packages in the virtual environment.
To check which python you are using use which python command and to check installed packages use pip3 list
I hope this will clear your doubt.

Python3 virtualenv installs python2

I am not sure what is wrong but I can't seem to get python3 in a virtualenv environment. I tried upgrading my ubuntu and updating all the packages - but no luck:
python3 -m virtualenv ENV
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/ramin/projects/buybulkamerica/ENV/bin/python2
Also creating executable in /home/ramin/projects/buybulkamerica/ENV/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
What can I do to ensure that virtualenv installs python3 instead of python2?
First, uninstall existing virtualenv.
sudo apt-get remove --purge python-virtualenv if you installed it using a package manager.
pip uninstall virtualenv if you have installed it using pip.
pip3 uninstall virtualenv if you have installed it using pip3.
Any one of the above commands will work.
Now install virtualenv again. Since you want python3, you need to run the following command.
pip3 install virtualenv
That should do the trick. Now when you create a new virtualenv, it will use python3.
There may be a better way but I had the same problem and after not finding any solution I tried this and it worked.
After you have installed virtualenv using pip, it doesn't matter if you used pip or pip3 if you give the location of your python3 installation to the virtualenv command, like this.
Create new virtualenv
virtualenv --python=/usr/bin/python3.6 environmentname
Access virtualenv
source /environmentname/bin/activate
If this doesn't work, use complete path from pwd
source /complete/path/to/environmentname/bin/activate
Stop virtualenv
deactivate

How to install a python module in ONLY virtualenv?

I am able to install python module in virtualenv but it is accessible outside the virtualenv as well. How to restrict its usage in virtualenv?
I went to the virtualenv path and then typed activate. It got activated as I could see root at the beginning.
And then I used the command pip install module_name
Activated the virutal env and then deactivated it.
When I activated the virtual env I was able to import the module.
When I deactivated it still the module was easily imported.
I assume the module was installed globally. I want it to be installed only in virtual env and should not be accessible outside.
The virtualenv tool is basically used to isolate the dependencies required for multiple projects. The python version installed within virtualenv won't be visible in the global directory.Try following the below steps properly in the virtualenv.
pip install virtualenv
cd my_project_folder
virtualenv venv
virtualenv -p /usr/bin/python3.5 venv
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
source venv/bin/activate
[Vitualenv Link:][1]
http://docs.python-guide.org/en/latest/dev/virtualenvs/
virtualenv -p /usr/bin/python3 name_of_venv #to create venv
source name_of_venv/bin/activate #to activate venv
pip install module_name #to install module
It is not visible from the outside. As soon as you type deactivate it is no longer importable.

Resources