0

I want to import business code from my test folder.

I installed my code using python setup.py install, which copy the code into the anaconda site-package folder. But I want to test my code in my dev. directory, as I don't want to constantly install my code to test it after any small change.

I am using Anaconda and Spyder IDE.

Here is my dir structure:

dev
└── myproject
    ├── setup.py
    ├── myproject
    │   ├── __init__.py
    │   └── myproject.py
    └── test
        ├── __init__.py
        └── test_importprojecthere.py

I took it from here: Running unittest with typical test directory structure

For now, I'm simply trying to import a function.

# dev: projectfile.py
def hello():
    print('Hello world!')

Here is where I call it from.

# dev: test_importprojecthere.py
from myproject.projectfile import hello # Use installed package.
hello()

more information:

Florian Fasmeyer
  • 795
  • 5
  • 18

1 Answers1

0

When you are creating packages, you do not want to install them in your site-packages.

Do not use

  • python setup.py install

Instead, use

  • python setup.py develop or...
  • pip install -e .

Those commands install your setup using symlinks. Instead of creating a copy, it links to your actual project. So when you modify it, it's being modified instantly everywhere.

Thanks to that, you will not have to update/install or do anything to run your unit tests.

-- "But now, every time I modify my package, everything using it might break. I don't want to break my production code!"

Do not worry! This is the reason why we use virtual environments. When you create a package, make sure you create a virtual environment, that you might name, by example "myproject_dev".

In the dev. env. use python setup.py develop so that you can develop on it.

In the prod. env. use python setup.py install, so that you use a copy, that will not suddenly change. You won't modify it, because in prod, you consume your package, you don't develop on it! Keeps things simple an safe.

Florian Fasmeyer
  • 795
  • 5
  • 18