wthrift
wthrift

Reputation: 43

How to install python modules from sources onto a virtual environment

As I understand it you install a module from sources with pip install -e /my_module

When I do this pip show -f my_module | grep Location gives ~/my_module as the location instead of my virtual environment's site-packages

I have my virtual environment activated when I install the module. I've also tried moving my module's folder into my virtual environment's site-packages but it installs it in site-packages/my_module instead of just site-packages.

I am doing this so that I can edit an existing module to work with my data's shape.

What is the correct way to install a module from sources onto a virtual environment?

Upvotes: 2

Views: 5146

Answers (3)

muchtarsp
muchtarsp

Reputation: 237

Based on this article:

  1. Activate the virtual environment:

     source your_virtual_env/bin/activate
    
  2. Go to the library folder that you would install:

     cd /path/to_your_library
    
  3. Run command:

     python -m pip install .
    

If there is no error during building from source, you will get the package installed in your virtual environment.

Upvotes: 0

Nujufas
Nujufas

Reputation: 802

Assuming you have a setup.py setup file in your library

Step 1: Activate your virtual environment.

Step 2: Navigate to your library source folder.

Step 3: Install the library usually with 'python setup.py install'.

Upvotes: 1

phd
phd

Reputation: 94827

As I understand it you install a module from sources with pip install -e /my_module

No, not exactly. pip install -e installs in "editable", i.e. development mode. Instead of copying installed files into site-packages/ directory pip configures site-packages/ and your sources so that python imports your modules from you source directory. That way you can edit the modules and the changes are immediately available to python. Without -e pip would install the package in the usual way — by copying it to site-packages/ so if you edit your code you need to reinstall.

When I do this pip show -f my_module | grep Location gives ~/my_module as the location instead of my virtual environment's site-packages

Yes, that's how pip install -e works. If you want your code to be copied to virtual environment's site-packages/ don't use -e.

I am doing this so that I can edit an existing module to work with my data's shape.

Then you certainly need -e so that your modules can be imported from the source directory.

What is the correct way to install a module from sources onto a virtual environment?

Both pip install and pip install -e are correct, they are for different use cases.

Upvotes: 1

Related Questions