Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing my own python module inside a virtual environment

What I have:

  • local Python3 files that I want to turn into a module test_module

  • test_module folder containing an empty __init__.py, a setup.py file (see below) and subdirectories with several source files

What I want:

  1. continuously work on and improve test_module locally

  2. have an easy way to install test_module and all its dependencies locally in my own virtual environment (created using python3 -m venv my_environment)

  3. run files that make use of the module via python myexample.py, without having to take care of adapting my local PYTHONPATH variable each time i enter or exit the my_environment

  4. share my python code with others via git, and allow them to install their code locally on their machines using the same procedure (as simple as possible)

  5. learn best practices on how to create my own module

How I'm doing it at the moment:

  • pip freeze > requirements.txt and pip install -r requirements.txt for installing dependencies

  • adding export PYTHONPATH="${PYTHONPATH}:." to my_environment/bin/activate, to have my own module in the search path (as found here: How do you set your pythonpath in an already-created virtualenv?)

I'd like to know if there are "cleaner" solutions based on setup.py, possibly involving something like pip install ./test_module or similar that takes care of 2.-3. automagically.

My current setup.py file looks as follows

 from setuptools import setup

 setup(
    name='test_module',
    version='0.1',
    description='Some really good stuff, that I am still working on',
    author='Bud Spencer',
    author_email='[email protected]',
    packages=['test_module'],  # same as name
    install_requires=['numpy', 'scipy', 'sklearn', 'argparse'], # external packages as dependencies
  )
like image 862
mattu Avatar asked Sep 27 '18 23:09

mattu


People also ask

Can Python packages be installed in virtual environment?

As long as your virtual environment is activated pip will install packages into that specific environment and you'll be able to import and use packages in your Python application.

How do I install a Python module?

You can install modules or packages with the Python package manager (pip). To install a module system wide, open a terminal and use the pip command. If you type the code below it will install the module. That will install a Python module automatically.

Should I use VENV or virtualenv?

These are almost completely interchangeable, the difference being that virtualenv supports older python versions and has a few more minor unique features, while venv is in the standard library.


1 Answers

It sounds like you want to run pip install -e <path/url> from within your virtual env, which will install a package (with a setup.py file as you have) from either a local path or a Git repo. See https://pip.pypa.io/en/stable/reference/pip_install/#vcs-support for an explanation on the syntax of the latter.

Example:

pip install -e git+https://github.com/me/test_module/#egg=test-module

If you have already installed and want to pull the latest code from the repo, add an --upgrade switch to the above.

like image 77
Svet Avatar answered Nov 07 '22 01:11

Svet