Save all currently installed packages in anaconda to a file - python-3.x

I want to make a .txt file with all the python packages my environment is using, and include it in the git repo, so that anyone who needs to run the code, can just make an environment, install all the packages from the list and use it.
I have two questions, first, how can I create that .txt files with all the installed packages? Second, how can someone with the .txt file install everything from it (using pip3?) in their fresh anaconda environment?

After activating your environment, you can do this:
pip freeze > requirements.txt
And to install all these packages in a fresh environment:
pip install -r requirements.txt
Hope this helps!

Related

I have completely messed up my Python Env and need help to start fresh

Long story short, I messed with my Python environment too much (moving files around, creating new folders, trying to reinstall packages, deleting files etc.) My google package doesn't work anymore. Everytime I try to import the package, it says it can't find the module, even though I did a pip install.
I was wondering how I could do a hard reset/delete python off my computer and reinstall it.
Thanks.
is your pip working try extracting the required modules.
pip freeze > requirements.txt
create new Venv
pip install -r requirements.txt
I figured it out. My pip was installing to a site packages folder inside a local folder, while my jupyter notebook was trying to pull from the anaconda site packages folder.

Dependency file in python

I am new in python. I am creating an API in python using flask-restful. I have created APIs in java.In java we have pom.xml file for dependencies...is there any for python and flask-restful
Yes. In python We generally make requirements.txt so anyone who wants to download all the requirements can simply run the command
pip install -r requirements.txt
so if you are using virtualenv you can simply do
pip freeze > requirements.txt
Otherwise you need to add all the dependencies manually & the requirements.txt file will looks like
decorator==4.3.0
defusedxml==0.5.0
entrypoints==0.2.3
Flask==1.0.2
google==2.0.1
Note: It's just for example.
I would recommend using pipenv.
In Java, you need to know where your library dependencies are located and they usually are downloaded once for each project as you need them. In other words, each project will have their own set of plugins. This is also the same for non-global NPM packages (package.json), Ruby gems (Gemfile), and so on.
But in Python, everything you install with pip is global. Anything you install with pip will make your system Python installation messy at best and at worst will not be portable between developer machines. We get around this problem with the concept of a virtual environment which more or less is a copy of whatever Python version you're using self-contained to a project.
pipenv works pretty similarly to npm.
You initialise with pipenv --three and use pipenv install Flask (for example) to install packages and keep track of them in Pipfile.toml and a lock file. You can then clone it on another computer and pipfile install to install all dependencies.
If this tool does not work for you, you may also want to try pyenv and virtualenv and use a requirements.txt file as suggested by Rahul.
Hope that helps!

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.

Python Packages Installation from local directory

There is a need to install python packages on machine without internet connection
I used pip download to download the packages and their dependencies
I copied all the dependencies to the offline machine
I run pip from the local python packages repository using
pip install *
package with dependencies are trying to access the internet to download their dependencies even that they are locate in the same directory
I would like to avoid the requirement.txt file and would like it to install all the packages from the local directory with their dependencies.
Is there any way to do so?
It's possible to download the wheels directly for each package, and once you have them on the machine you can run pip install name-of-wheel.whl and it will install them without routing to pypi.
You can use on the online machine:
pip download -r requirements.txt
to download package without installing them.
Then, on the offline machine:
pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt
Source: Python Packages Offline Installation

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.

Resources