All you need to do is run
pip install /opt/mypackage
and pip will search /opt/mypackage for a setup.py or pyproject.toml, build a wheel, then install it.
The problem with using the -e flag for pip install as suggested in the comments and this answer is that this requires that the original source directory stay in place for as long as you want to use the module. It's great if you're a developer working on the source, but if you're just trying to install a package, it's the wrong choice.
Alternatively, you don't even need to download the repo from Github first. pip supports installing directly from VCS (version control systems) repos like git using a variety of protocols including HTTP, HTTPS, and SSH, among others. See the docs for examples.
To see all of the pip install command options, run
pip install --help
Answer from MattDMo on Stack OverflowAll you need to do is run
pip install /opt/mypackage
and pip will search /opt/mypackage for a setup.py or pyproject.toml, build a wheel, then install it.
The problem with using the -e flag for pip install as suggested in the comments and this answer is that this requires that the original source directory stay in place for as long as you want to use the module. It's great if you're a developer working on the source, but if you're just trying to install a package, it's the wrong choice.
Alternatively, you don't even need to download the repo from Github first. pip supports installing directly from VCS (version control systems) repos like git using a variety of protocols including HTTP, HTTPS, and SSH, among others. See the docs for examples.
To see all of the pip install command options, run
pip install --help
You were looking for help on installations with pip. You can find it with the following command:
pip install --help
Running pip install -e /path/to/package installs the package in a way, that you can edit the package, and when a new import call looks for it, it will import the edited package code. This can be very useful for package development. Only use the -e flag if you need to edit the package's source code.
Further information about local installs and the -e/--editable flag of pip and its caveats is available in the official pip "Local project installs" and setuptools "Development Mode (a.k.a. "Editable Installs)" documentation chapters. The latter also lists a range of limitations of editable installs.
Installing Python packages from local file system folder to virtualenv with pip - Stack Overflow
Where do i store my python package locally if i want to access it from anywhere?
software - What is the simplest way to do a user-local install of a python package? - Computational Science Stack Exchange
How does `pip install -e` work? Is there a specific file it looks for and installs from that? What does it do when you give it no arguments?
Videos
If you're using sudo with pip3 command, package will install to /usr/local/bin/mysqlclient directory which will be accessed by all users(i.e. installing globally).
Whereas, without sudo it will install to ~/.local/bin/mysqlclient directory and only access current user(i.e. installing locally).
But, try to prefer virtual environments to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has. It's handy for large sized projects.
You only use sudo or elevated permissions when you want to install stuff for the global, systemwide Python installation with pip or pip3. Otherwise install python packages locally with pip3 install --user <package> (e.g. pip3 install --user mysqlclient).
Malicious packages are occasionally found on PyPI, the official third-party repository for software for the Python programming language. It is best to use a Python virtual environment to isolate packages that you install with pip/pip3. The virtualenv utility creates virtual Python instances, each invokable with its own Python executable. Each instance can have different sets of modules. Virtual Python instances can also be created without root access.
What about::
pip install --help
...
-e, --editable <path/url> Install a project in editable mode (i.e. setuptools
"develop mode") from a local project path or a VCS url.
eg, pip install -e /srv/pkg
where /srv/pkg is the top-level directory where 'setup.py' can be found.
I am pretty sure that what you are looking for is called --find-links option.
You can do
pip install mypackage --no-index --find-links file:///srv/pkg/mypackage
Im relatively new to python programming professionally (just over a year now) and i am at a point where i have code i have made that i want to use elsewhere. If i copy and paste the functions/classes, then any changes i make to them id need to make to them all. I understand the proper way of doing this is by making it a package.
I have made my code into a package, which i have working. But at the moment that package folder has to be in the save folder as the code im writing.
After some googling, ive found that you put them in the folder: /Library/Frameworks/Python.framework/Versions/3.9/lib
When i put it here, i still cant access it?
I am on mac os
I ran which python3 which is where i got the above path...
Any help would be great :)
Python (as of 2.6 and 3.0) now searches in the ~/.local directory for local installs, which do not require administrative privileges to install, so you just need to point your installer to that directory.
If you have already downloaded the package foo and would like to install it manually, type:
cd path/to/foo
python setup.py install --user
If you are using easy_install and would like the package downloaded and installed:
easy_install --prefix=$HOME/.local/ foo
Update by RafiK
pip install --user foo
The following answer is provided for historical purposes: It's a little more work if you are using pip to download and install:
pip install --install-option="--prefix=$HOME/.local" foo
Even though I like Python as a language, distributing Python packages is a mess. I always find people not familiar with Python struggling with it.
Next to the user-local install as outlined by Aron (using --user, or --prefix), another option is EasyBuild (http://hpcugent.github.com/easybuild/). Not only for Python packages, but for any (scientific) software package. Once EasyBuild has support for it, building and installing a software package is basically a single command.
For a list of software packages currently supported, see https://github.com/hpcugent/easybuild/wiki/List-of-supported-software-packages.
Disclaimer: I am a developer of EasyBuild.
Weirdly enough, on Debian and Devuan the --prefix needed to target /usr/local is /usr.
Meanwhile, --prefix=/usr/local installs to /usr/local/local/bin ¯\_(ツ)_/¯
This:
pip install --prefix=/usr awscli
produces the desired outcome, namely:
- executable
/usr/local/bin/aws, python -c 'import botocore'working,- for all users of the container image.
Caveats
You'd best be sure you can guarantee the intersection of apt-installed and pip-installed python packages is, and will be, empty.
This is because both /usr and /usr/local form one "python installation"; the interpreter sees packages installed in both. (apt ones in /usr; pip ones in /usr/local).
Thus in PEP-668 terms, package shadowing may occur:
- system tool
foodepends oncommon == 1.*; - you pip-install
barthat pulls incommon >= 2.0; - system tool
foois now broken, becausecommonv1 has been shadowed.
To summarize the generic advice pertaining to this issue.
1. Use apt package if possible
For simple needs, you might not actually need pip.
As an example, (some version of) requests can be installed with
sudo apt install python3-requests
2. Use venv, virtual environment
It's rather simple to spin up a dedicated "python installation" a.k.a. venv with completely isolated set of packages.
#-- ensure the dependency module is there
sudo apt install python3-venv
#-- create a venv at path ./my-shiny-awesome-sandbox/
python3 -m venv my-shiny-awesome-sandbox
#-- activate it in this shell session
source my-shiny-awesome-sandbox/bin/activate
pip list #-- empty
pip install .... #-- installs to ./my-shiny-awesome-sandbox
Then you don't have to worry about interactions with system packages.
Instead, you worry about maintaining & activating the venv.
3. Use pipx
sudo apt install pipx
pipx run pycowsay moo
This conceptually maintains an implicit hidden venv.
Because pipx is oriented at apps (console_scripts entry points), it's not trivial to install libraries with it, that you could import in your own scripts.
4. Say --break-system-packages
sudo pip install --break-system-packages awscli
This will install to /usr/local. You'll get both the entrypoint scripts, and the libraries.
A caveat is, you run the risk of intermixing apt packages with pip ones, if not careful. That's why the option name is intentionally scary.