Importing modules from a sibling directory for use with py.test

2024/10/5 21:24:07

I am having problems importing anything into my testing files that I intend to run with py.test.

I have a project structure as follows:

/ProjectName
|
|-- /Title
|    |-- file1.py
|    |-- file2.py
|    |-- file3.py
|    |-- __init__.py
|
|-- /test
|    |-- test_file1.py

I have not been able to get any import statements working with pytest inside the test_file1.py file, and so am currently just attempting to use a variable declared in file_1.py and print it out when test_file1.py is run.

file1.py contains:

file1_variable = "Hello"

test_file1.py contains:

import sys
import os
sys.path.append(os.path.abspath('../Title'))
import file1def test_something():assert file1.file1_variable == "Hello"print(file1.file1_variable)

The import statement from my testing file is taken from https://stackoverflow.com/a/10272919/5477580 which works by editing the PYTHONPATH variable. This allows me to run the test_file1.py script and successfully execute the print statement.

However, attempting to run py.test from the /ProjectName directory gives me an error saying ImportError: No module named 'file1'

Is there some way in which I can structure things better so that pytest will be possible? Do I need to add something to my __init__.py file?

Answer

No you don't need to add anything to __init__.py. This file tells python to treat the parent directory as module which can be imported as described here. Just add it to your Title directory and import it like

from ..Tiltle.file import file1_variable

here we are moving 1 hierarchy above in the directory just like cd ..

Note .. is for reaching the Title package from test directory. If you need to run your file from ProjectName directory you will have to do

from Tiltle.file import file1_variable
https://en.xdnf.cn/q/70443.html

Related Q&A

Uploading and processing a csv file in django using ModelForm

I am trying to upload and fetch the data from csv file uploaded by user. I am using the following code. This is my html form (upload_csv1.html):<form action="{% url myapp:upload_csv %}" me…

Plotting Multiple Lines in iPython/pandas Produces Multiple Plots

I am trying to get my head around matplotlibs state machine model, but I am running into an error when trying to plot multiple lines on a single plot. From what I understand, the following code should…

libclang: add compiler system include path (Python in Windows)

Following this question and Andrews suggestions, I am trying to have liblang add the compiler system include paths (in Windows) in order for my Python codeimport clang.cindexdef parse_decl(node):refere…

Pako not able to deflate gzip files generated in python

Im generating gzip files from python using the following code: (using python 3)file = gzip.open(output.json.gzip, wb)dataToWrite = json.dumps(data).encode(utf-8)file.write(dataToWrite)file.close()Howev…

Best way to have a python script copy itself?

I am using python for scientific applications. I run simulations with various parameters, my script outputs the data to an appropriate directory for that parameter set. Later I use that data. However s…

How to convert dictionary to matrix in python?

I have a dictionary like this:{device1 : (news1, news2, ...), device2 : (news 2, news 4, ...)...}How to convert them into a 2-D 0-1 matrix in python? Looks like this:news1 news2 news3 news4 device1 …

Ubuntu 16.04 - Why I cannot install libtiff4-dev?

Following this tutorial, I am trying to install the OpenCV 3 with Python on Ubuntu 16.04.At the step of entering $ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-devI got this me…

Histogram update in a for loop with matplotlib.pylab

I am trying to update in for loop a histogram data. but I dont know how to make it. I tried with set_data but it is not working. here is the code:plt.ion() ax=plt.subplot(111) [n,X, V]=ax.hist(range(MA…

How to remove a section from an ini file using Python ConfigParser?

I am attempting to remove a [section] from an ini file using Pythons ConfigParser library.>>> import os >>> import ConfigParser >>> os.system("cat a.ini") [a] b = c…

why does this script not work with threading python

so ive been trying to ifnd a way to access task manager. Ive tried a few methods including the wmi module and the windows tasklist but neither suit my need. wmi is way too slow and tasklist becomes to…