Problem with importing files into diferenter directories

1

I have a problem with using different files in different directories, this is the directory tree of my project:

DeatHash
├── deathash
│   ├── deathash.py
│   ├── dictionaries
│   │   ├── dictionary_test.txt
│   │   └── dictionary.txt
│   ├── files
│   │   ├── archive_test.txt
│   │   ├── cesar.txt
│   │   └── vigenere.txt
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-36.pyc
├── doc
├── __init__.py
├── LICENSE
└── test
    └── test_deathash.py

I need to access the main module deathash.py , path:

  

DeatHash / deathash / deathash.py

from the test_deathash.py module, path:

  

DeatHash / test / test_deathash.py

I have already tried to put init.py in the directories (obs: put it with _ (underline) before and after 'init', with .py at the end), also tried using test_deathash.py >, the code

from deathash.deathash import *

Another solution I found is to add to the path the directory that contains the function I'm going to use, but apparently this solution would be ineffective if the project is taken to another directory, which may happen, so if you know of another solution to the problem please help me.

    
asked by anonymous 09.03.2018 / 22:40

2 answers

1

Your "deathash" as it stands is a Python "package" (a collection of several modules).

As a rule, what Python does to find packages is:

  • It looks for the name of the package in the folder in which it is running (where you typed "python" in the terminal, or in the folder where the ".py" file you click, if not the terminal)
  • In the default Python library (installed along with the current interpreter, which can be virtualenv)
  • In the "site-packages" folder that comes with the interpreter. In this folder there may also be some special files that point to packages installed elsewhere - except that these files are complicated and created by "distutils" or "setuptools" from code that usually stays in a setup.py file in your project .
  • in any folder that is in the environment variable PYTHONPATH .
  • After the program is running, all these folders are in order in the list that is displayed in the sys.path variable. So simply putting the folder where the "deathash" folder is manually in the sys.path list will work (with .append or .insert normal)

    But this is not the "cleanest" way to do this - since in that case, your test will depend on finding the core package "alone."

    The "most correct" is that your deathash is installed - either in the main Python or in the current virtualenv - but this requires the creation of an appropriate "setup.py" file.

    This is not difficult - a mimio setup.py can have about 10 lines - see documentation here link

    For the example of 4 lines of setup.py of the above link to work, you just need to: install setuptools , write your setup.py, and call it with the "develop" option: python setup.py develop - this will create a permanent link in the site-packages of your Python environment for your package, and it will always be found.

    And finally, the simplest form of all, which does not require any changes, run your test script by calling it from the folder where the deathhash and tests directories (in this case DeatHash ) : this way both deathash and tests will be visible as packages. (the rule "1" above: it looks for the package in the folder from which Python is running)

    python test/test_deathash.py
    

    Or, if you're using pytests :

    py.test test
    
        
    10.03.2018 / 05:01
    0

    For those who also have this doubt, it is good to know that you were right to put the __init__.py files in the folders. Without that it would not work.

    For your specific case, in the file test_deathash.py start as follows:

    import sys
    sys.path.append('../deathash/')
    from deathash import *
    
        
    10.03.2018 / 00:01