4

I have defined a few classes in a Python file. I know I can import it into a given script so long as that file is in the same directory as my script; however, I would like to be able to install that file as a package so I can import it without having to place it in the current directory everytime I want to use it.

Essentially, is there a way to pip install a package you've written yourself so it can be imported in a Python script without existing in the same directory as the script?

Any help or advice in this arena would be greatly appreciated Thanks!

Connor
  • 41
  • 1
  • Possible duplicate of [Installing Python packages from local file system folder with pip](http://stackoverflow.com/questions/15031694/installing-python-packages-from-local-file-system-folder-with-pip) – Todor Minakov May 02 '17 at 19:11

1 Answers1

2

You will need this

a minimal example for your setup.py:

from setuptools import setup
setup(
  name='foo',
  version='1.0',
  py_modules=['foo'],
)

Your folder should look like:

[some_folder]
  | [foo]
  |   | __init__.py
  |   | the_code_you_already_wrote.py
  |
  | setup.py

in the __init__.py you can simply write:

from the_code_you_already_wrote import *
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71